Java JToggleButton未在GUI中显示
我试图在Java JToggleButton未在GUI中显示,java,swing,jpanel,layout-manager,null-layout-manager,Java,Swing,Jpanel,Layout Manager,Null Layout Manager,我试图在JPaneltotalGUI中添加一个矩形和30个JButton,但只显示了一个矩形 public class SelectSeat { static JFrame frame; int a; public JPanel createContentPane() throws IOException { JPanel totalGUI = new JPanel(); RectDraw rect= new RectDraw
JPanel
totalGUI中添加一个矩形和30个JButton
,但只显示了一个矩形
public class SelectSeat {
static JFrame frame;
int a;
public JPanel createContentPane() throws IOException
{
JPanel totalGUI = new JPanel();
RectDraw rect= new RectDraw();
rect.setPreferredSize(new Dimension(330,35));
totalGUI.setLayout(null);
totalGUI.setBackground(Color.WHITE);
totalGUI.add(rect);
Dimension d = rect.getPreferredSize();
rect.setBounds(100, 20, d.width, d.height);
JToggleButton[] ButtonList = new JToggleButton[30];
for(int i = 0; i < 30; i++) {
a=i+1;
ButtonList[i]= new JToggleButton(""+a);
totalGUI.add(ButtonList[i]);
}
totalGUI.add(rect);
return totalGUI;
}
void setVisible(boolean b) {
// TODO Auto-generated method stub
}
static void createAndShowGUI() throws IOException
{
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("Seat Selection");
//Create and set up the content pane.
SelectSeat demo = new SelectSeat();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(535, 520);
frame.setLocation(500,220);
frame.setVisible(true);
}
private static class RectDraw extends JPanel
{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GRAY);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLUE);
g.drawString("Movie Sceen", 130, 20);
}
}
}
选择座椅
next.addActionListener(new ActionListener() // go to SelectSeat class
{
public void actionPerformed(ActionEvent e)
{
SelectSeat back = new SelectSeat();
back.setVisible(true);
setVisible(false);
frame.dispose();
}
});
public class SelectSeat {
public static void main(String[] atrg) throws IOException {
JFrame meinJFrame = new JFrame();
meinJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
meinJFrame.setSize(800, 600);
meinJFrame.setVisible(true);
meinJFrame.add(new RectDraw());
JPanel over = new JPanel(new GridLayout(2, 1));
over.add(new RectDraw());
meinJFrame.setTitle("Cinemax-Beispiel");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6, 5));
for (int i = 1; i <= 30; i++) {
panel.add(new JToggleButton("Seat :" + i));
}
over.add(panel);
meinJFrame.add(over);
}
public void setVisible(boolean b) { // auto-generated
// TODO Auto-generated method stub
}
}
class RectDraw extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.drawString("Movie Sceen", 130, 20);
}
}
public class选择座位{
公共静态void main(字符串[]atrg)引发IOException{
JFrame meinJFrame=新JFrame();
meinJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
meinJFrame.setSize(800600);
meinJFrame.setVisible(true);
添加(新的RectDraw());
JPanel over=新JPanel(新网格布局(2,1));
添加(新的RectDraw());
meinJFrame.setTitle(“Cinemax Beispiel”);
JPanel面板=新的JPanel();
面板设置布局(新网格布局(6,5));
对于(inti=1;i来说,关键是您没有很好地组织jpanel
您应该以这种形式组织面板:
----JPanel----------------------------|
| ---Screen el---------------------- |
| | txt | |
| | | |
| ----------------------------------- |
| ---seats -------------------------- |
| |seat1 seat2 .. ... ... ... | |
| |seat11 seat12 .. ... ... .. | |
| |... ... ... | |
| ----------------------------------- |
---------------------------------------
public class Mango {
public static void main(String[] atrg) throws IOException {
JFrame meinJFrame = new JFrame();
meinJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
meinJFrame.setSize(800, 600);
meinJFrame.setVisible(true);
meinJFrame.add(new RectDraw());
JPanel over = new JPanel(new GridLayout(2, 1));
over.add(new RectDraw());
meinJFrame.setTitle("Cinemax-Beispiel");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6, 5));
for (int i = 1; i <= 30; i++) {
panel.add(new JToggleButton("Seat :" + i));
}
over.add(panel);
meinJFrame.add(over);
}
}
class RectDraw extends JPanel {
private static final long serialVersionUID = -6507364980760764676L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.drawString("Movie Sceen", 130, 20);
}
}
----JPanel----------------------------|
|---屏幕el------------------|
|| txt||
| | | |
| ----------------------------------- |
|---座位------------------|
||座位1座位2………….||
||座位11座位12|
| |... ... ... | |
| ----------------------------------- |
---------------------------------------
公共级芒果{
公共静态void main(字符串[]atrg)引发IOException{
JFrame meinJFrame=新JFrame();
meinJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
meinJFrame.setSize(800600);
meinJFrame.setVisible(true);
添加(新的RectDraw());
JPanel over=新JPanel(新网格布局(2,1));
添加(新的RectDraw());
meinJFrame.setTitle(“Cinemax Beispiel”);
JPanel面板=新的JPanel();
面板设置布局(新网格布局(6,5));
对于(int i=1;i实例不应该以大写字母开头-它应该是buttonList
。在这种情况下,我可能会创建一个带有GridLayout的内部JPanel,将所有这些单选按钮添加到其中,然后将面板添加到totalGUI
。最后,我会将totalGUI
布局设置为SpringLayout,以便矩形和按钮的高度将根据JFrame的大小进行调整。1)为了更快地获得更好的帮助,请发布or。该代码至少需要import语句和main(String[])
方法才能将其显示在屏幕上,以便成为MCVE的C、V或E。2)以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则具有更大的宽度和高度。@AnderThompson是否必须具有main(String[])
?我有许多没有main
的类,但它们仍然可以运行。“我有许多没有main编写的类,但它们仍然可以运行。”如果是这样的话,你的IDE必须在幕后创建一个main方法。“是否必须有main(String[])
?”才能成为MCVE,是的。尽管我在开始时说了“为了更快地获得更好的帮助…”我没有提到的是,实际上有两个接近的原因提到“没有MCVE”作为原因的一部分。我只是选择了其中一个原因,如果还有4个人同意,这个问题就结束了。因此,我将把是否是“必须”的问题留给你来决定。你现在发布的是一些代码片段。有些甚至不是一个完整类。发布MCVE的部分想法是让人们更容易看到问题,但这需要多次复制/粘贴,添加导入,并为action listener所在的任何内容充实一个完整的类。我在第一篇评论中提到了导入,您似乎没有阅读或不理解MCVE/SSCCE ar贴士。我有更好的事情要做,而不是在链接中清楚地列出填鸭式的说明,主要是帮助那些能够发布我们可以使用的代码的人。我尝试运行你的代码,但JToggle按钮没有显示。检查我编辑的帖子。谢谢你的屏幕截图完成了吗??