[英]Why won't my JPanel show?
So I'm just checking and when I click my button it won't show my JPanel, any idea why? 所以我只是检查一下,当我单击我的按钮时,它不会显示我的JPanel,为什么?
Thanks. 谢谢。
I want the third class to show, really do appreciate the help - Thanks allot. 我想让三等班的同学们看到,真的很感谢您的帮助-感谢分配。
First class - JFrame class. 头等舱-JFrame类。
import javax.swing.JFrame;
public class Frame {
public static void main(String[] args ) {
JFrame frame = new JFrame("JFrame Demo");
Panel panel1 = new Panel();
frame.add(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setVisible(true);
}
}
Second class - Panel 1 二等座-小组1
import javax.swing.JPanel;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Panel extends JPanel{
public Panel() {
setLayout(null);
final Panel2 panel2 = new Panel2();
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
panel2.setVisible(true);
}
});
btnNewButton.setBounds(62, 197, 224, 122);
add(btnNewButton);
}
}
Third class - Panel 2 (I want this to show) 第三类-第2面板(我希望这个显示)
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.CardLayout;
import javax.swing.JTextField;
public class Panel2 extends JPanel {
private JTextField textField;
public Panel2() {
setLayout(null);
setVisible(true);
textField = new JTextField();
textField.setBounds(84, 84, 290, 77);
add(textField);
textField.setColumns(10);
}
}
You never add panel2
to anything. 您永远不会将
panel2
添加到任何内容。 A JPanel
isn't like a JFrame
where setVisible
makes it magically appear. JPanel
不像setVisible
使它神奇地出现的JFrame
。 You need to add it to a container. 您需要将其添加到容器中。 Just add it to your
Panel
. 只需将其添加到您的
Panel
。
Also avoid using null
layouts. 另外,请避免使用
null
布局。 Learn to use Layout Managers 学习使用布局管理器
Also see Initial Threads . 另请参阅初始线程 。 You want to run your swing apps from the Event Dispatch Thread like this
您想像这样从事件调度线程运行swing应用程序
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { new Frame(); } }); }
This looks like a case where you may have been trying to do something along the lines of what a CardLayout
achieves. 这看起来像是您尝试按照
CardLayout
实现的功能进行某些操作的情况。 See this example for a basic use. 基本用法请参见此示例 。 Also see How to Use Card Layout
另请参阅如何使用卡布局
In the second class, after the second line in the constructor, have you tried? 在第二个类中,在构造函数的第二行之后,您是否尝试过?
add(panel2);
See if this works. 看看是否可行。
Modify Panel.java to look like below. 修改Panel.java使其如下所示。 Tell me if this is good for your needs:
告诉我这是否满足您的需求:
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Panel extends JPanel{
Panel2 panel2 = null;
JButton btnNewButton = null;
public Panel() {
setLayout(null);
panel2 = new Panel2();
panel2.setBounds(5,5,300,500);
add(panel2);
showPanel2(false);
btnNewButton = new JButton("New button");
btnNewButton.setBounds(62, 197, 224, 122);
add(btnNewButton);
showButton(true);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showButton(false);
showPanel2(true);
}
});
}
public void showPanel2(boolean bshow)
{
panel2.setVisible(bshow);
}
public void showButton(boolean bshow)
{
btnNewButton.setVisible(bshow);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.