我最近一直在开发软件应用程序,这些应用程序大多是非常基础的,而且我遇到了一个问题。
我正在开发的应用程序有许多不同的菜单和屏幕,我希望JFrame在单击按钮时交替显示。
考虑到这是大多数应用程序似乎正在实现的特性,因此我可以看到令人惊讶的很少信息,这让我怀疑我的方法是否完全失效,但是有一些示例代码说明了这种方法。
因此,我的问题是:( a)实现这一目标的最佳方法是什么;( b)我当前的代码有什么问题?然而,前一个问题是最重要的。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
JFrame frame;
JButton nextButton = new JButton ("Next Screen");
public void setup() {
frame = new JFrame();
frame.setVisible(true);
frame.add(new PanelOne());
frame.pack();
}
public class PanelOne extends JPanel { {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JLabel("Label One"));
this.add(new JLabel("Label Two"));
this.add(new JLabel("Label Three"));
this.add(new JLabel("Label Four"));
this.add(new JLabel("Label Five"));
JButton button = new JButton("Next Screen");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
swapPanel();
}
});
this.add(button);
} }
public class PanelTwo extends JPanel {{
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JButton("Button One"));
this.add(new JButton("Button Two"));
this.add(new JButton("Button Three"));
this.add(new JButton("Button Four"));
this.add(new JButton("Button Five"));
}}
protected void swapPanel() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.removeAll();
frame.add(new PanelTwo());
frame.invalidate();
frame.revalidate();
}
});
}
public static void main (String[] args) {
Demo demo = new Demo();
demo.setup();
}
}
发布于 2015-04-16 01:53:33
用这个作为你的代码..。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
JFrame frame;
JButton nextButton = new JButton ("Next Screen");
PanelOne p = new PanelOne();
public void setup() {
frame = new JFrame();
frame.setVisible(true);
frame.add(p);
frame.pack();
}
public class PanelOne extends JPanel { {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JLabel("Label One"));
this.add(new JLabel("Label Two"));
this.add(new JLabel("Label Three"));
this.add(new JLabel("Label Four"));
this.add(new JLabel("Label Five"));
JButton button = new JButton("Next Screen");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
swapPanel();
}
});
this.add(button);
} }
public class PanelTwo extends JPanel {{
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JButton("Button One"));
this.add(new JButton("Button Two"));
this.add(new JButton("Button Three"));
this.add(new JButton("Button Four"));
this.add(new JButton("Button Five"));
}}
protected void swapPanel() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.remove(p);
frame.add(new PanelTwo());
frame.invalidate();
frame.revalidate();
}
});
}
public static void main (String[] args) {
Demo demo = new Demo();
demo.setup();
}
}
希望能帮上忙..。
https://stackoverflow.com/questions/29671024
复制