我很陌生.Java Swing,我正在将CardLayout用于我的应用程序.在此应用程序中,用户可以创建活动,并在其他JPanel中查看现有活动.
我无法更新卡(标签所在的位置).如果我添加活动,当您重新启动应用程序时,它可以正常工作.我正在寻找一种不需要重启的方法.
简而言之,我想知道如何在运行时最好地重建/更新面板,以便显示新的标签数据.
提前致谢!
我有一个JTextField内部的JPanelA是其中的一部分CardLayout.当显示此A时,我想自动将焦点设置为JTextField(即光标在文本字段中闪烁,因此用户无需单击它以启用输入).我打过电话requestFocusInWindow()了关于JTextField在初始化对象,但似乎并没有工作.每次显示A时是否需要调用此方法?谢谢.
我CardLayout Manager在我的代码中遇到了困难.我无法弄清楚为什么我会得到这个例外.我在CardLayout.show()方法中传递一个字符串但仍然得到此错误.请帮忙.这是我的主要课程.
@SuppressWarnings("serial")
public class Main extends JFrame implements ActionListener {
final static String mainMenuPanel = "Main Menu";
final static String creditsPanel = "Credits";
final static String introPanel = "Introduction";
private CardLayout cardLayout = new CardLayout();
private JPanel cards = new JPanel(cardLayout);
public Main(){
//Create and set up the window.
super();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new CardLayout());
//this.pack();
this.setVisible(true);
this.setSize(new Dimension(800,600));
this.setLocationRelativeTo(null);
this.setTitle("Wise Frog Productions.");
cards.add(new IntroGamePanel(),introPanel);
cards.add(new MainMenu(),mainMenuPanel);
this.add(cards);
swapView(mainMenuPanel);
}
public void swapView(String s){
cardLayout.show(cards,s);
}
public …Run Code Online (Sandbox Code Playgroud) 我在3个单独的类中有3个窗口,我想使用cardLayout,这样当您单击下一个按钮时,将出现下一个窗口.如何将包含不同元素的JPanel添加到一个cardLayout?这是第一个窗口:(唯一不同的是背景 - 但它代表了我实际上是如何得到它的想法)
public class Window1 extends JPanel implements ActionListener {
static CardLayout cardLayout = new CardLayout();
public Window1() {
init();
}
private void init() {
JPanel jp = new JPanel(new BorderLayout());
JPanel jp2 = new Window2();
//JPanel jp3 = new Window3();
JLabel textLabel = new JLabel("Window1");
jp.setBackground(Color.GREEN);
jp.add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
jp.add(nextButton, BorderLayout.EAST);
setLayout(cardLayout);
add(jp, "string");
add(jp2, "string");
//add(jp3, "string");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next …Run Code Online (Sandbox Code Playgroud) 我之前的帖子解释了我在卡片布局中绘制图像的问题.我得到了有限的帮助,并没有解决问题.所以我在这篇文章中再次解释它,希望得到一些有效的帮助.
我正在尝试为我的java游戏制作标题屏幕.我制作了两个java文件.第一个文件加载cardlayout,下一个文件加载标题屏幕.但不知何故,在第二个文件中,我无法得到它来绘制我的背景图像.我没有收到任何错误消息.它根本没有画任何东西.
第一档:
import javax.swing.*;
import java.awt.*;
public class MainScreen extends JFrame{
public static CardLayout cardLayout = new CardLayout();//set a new cardlayout
// *** JPanel to hold the "cards" and to use the CardLayout:
static JPanel cardContainer = new JPanel(cardLayout);//some variable for the cardlayout
public static JComboBox cardCombo = new JComboBox();//some variable for the cardlayout
public static JPanel comboPanel = new JPanel();;//some variable for the cardlayout
Image background = Toolkit.getDefaultToolkit().createImage("data/images/title.png");//loads the background image
public MainScreen() {
JFrame frame = new …Run Code Online (Sandbox Code Playgroud) 我正在开发Java Swing桌面应用程序项目.该应用程序有大约15个GUI页面.我可以使用Layered Panes和Tabbed Panes将所有GUI组件放在一个类中.但那个班级将是巨大的.如果我可以将项目划分为几个较小的子项目并让每个项目都有一个或几个GUI页面,那将是一个想法.我可以单独处理每个子项目,并在所有子项目完成后将它们集成到一个应用程序中.我的问题是如何集成来自不同类的所有GUI页面,以便我可以在按钮点击的同时导航回来并强制不同的页面?由于子项目包含GUI页面,因此每个子页面都需要有一个JFrame.我如何在JFrame 1和JFrame 2之间切换回来并强制使一个可见而另一个不可见? 此问题 显示如何创建新的JFrame.但没有展示如何在JFrame中来回切换.
我正在使用一个简单的游戏JFrame.我做了一个简单的"开始"屏幕,基本上由a String和a组成JButton.我正在使用该actionPerformed(ActionEvent e)方法点击按钮.我不知道如何使用按钮点击更改卡片.这可能看起来像是一个简单的问题需要解决,但这种扭曲伴随着:我的主要JFrame,我的StartScreen和我发生游戏的JPanel都在不同的文件中.我的主文件Virus.java是我创建的文件JFrame.我的文件VirusGamePanel.java是游戏发生的地方.我的文件StartScreen.java是带按钮的屏幕.当玩家点击按钮时,我想将"卡片"更改为游戏屏幕.我怎样才能做到这一点?我的StartScreen.java文件:
package virus;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
public class StartScreen extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
JButton start = new JButton("Start");
public StartScreen(){
start.addActionListener(this);
start.setBounds(new Rectangle(400,300,100,30));
this.add(start);
}
public void paint(Graphics g){
super.paint(g);
g.setFont(new Font("Impact",Font.BOLD,72));
g.setColor(Color.MAGENTA);
g.drawString("Virus",275,300);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
//what to do here?
} …Run Code Online (Sandbox Code Playgroud) 我的应用程序由两个JPanels 组成,它们将是一张卡片MainPanel.代码如下.出于某种原因,面板不会出现.我非常感谢你的帮助:)
MainPanel.java:
public class MainPanel extends JPanel{
private final static String PANEL1 = "PANEL1";
private final static String PANEL2 = "PANEL2";
private static Panel1 panel1;
private static Panel2 panel2;
//private static CardLayout layout;
public MainPanel() {
super(new CardLayout());
panel1 = new Panel1();
panel2 = new Panel2();
getLayout().addLayoutComponent(PANEL1, panel1);
getLayout().addLayoutComponent(PANEL2, panel2);
((CardLayout) getLayout()).show(this, PANEL1);
}
}
Run Code Online (Sandbox Code Playgroud)
Main.java:
public class Main {
private static JFrame window;
public static void main(String[] args) {
window = new JFrame();
window.setContentPane(new …Run Code Online (Sandbox Code Playgroud) 我有一个主框架,我想用卡片布局在中心位置显示我的NewUser类的对象.这是我的主要课程
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo implements ItemListener {
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";
public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "cards".
NewUser newUser = …Run Code Online (Sandbox Code Playgroud) 我确定之前有人问过这个问题,但今天我的google-fu并不强大.
我有一个使用CardLayout作为其管理器的JFrame.当我在不使用开关的情况下切换到每个JPanel时,如何运行"开始"方法?
我用来将帧添加到布局的代码是:
/**
* Adds JPanels to the Card Layout.
* @param panel is the JPanel to add to the layout.
* @param windowName is the identifier used to recognise the Panel.
*/
public final void addToCards(final JPanel panel, final WindowNames windowName) {
view.getBasePanel().add(panel, windowName.getValue());
}
Run Code Online (Sandbox Code Playgroud)
我用来切换布局的代码是:
/**
* Method to change the JPanel currently visible on the BasePanel.
* @param windowName is the name of the JPanel to change to.
*/
public final void changePanel(final WindowNames windowName) { …Run Code Online (Sandbox Code Playgroud) cardlayout ×10
java ×10
swing ×10
jpanel ×3
2d-games ×1
awt ×1
exception ×1
image ×1
jtextfield ×1