1

我希望在我的 GUI 中显示一个包含 12 个按钮的面板。但由于某种原因,他们没有出现。我现在不知道我的代码有什么问题。我还没有在我的动作执行方法中添加任何东西,但我会(现在我只专注于让按钮显示)。那么有人可以告诉我出了什么问题吗?谢谢!

编辑* 这就是我的 GUI 类中的所有内容,我想要显示两个电梯对象画布(什么都没有显示)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUI extends JFrame implements ActionListener 
{
private static final Dimension PREF_SIZE = new Dimension(1000, 1000);

MyCanvas leftCanvas = new MyCanvas();
MyCanvas rightCanvas = new MyCanvas();
ArrayList<JButton> buttonList = new ArrayList<JButton>(); 
JPanel buttonPanel, leftPanel, rightPanel;

public GUI()
{
  super("Elevators");
    //setSize(800,800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //setVisible(true);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(1,3));

    leftPanel = new JPanel();
    leftPanel.add(leftCanvas);
    rightPanel = new JPanel();
    rightPanel.add(rightCanvas);

    buttonPanel = new JPanel();     
    buttonPanel.setLayout(new GridLayout(12,1));   
    buttonPanel.setSize(900,900);

    add(mainPanel); 

    for(int i=0; i<12; i++)
    {
        buttonList.add(new JButton(""+i));
        JButton btn = buttonList.get(i);
        buttonPanel.add(btn);
    }
    mainPanel.add(buttonPanel, BorderLayout.CENTER);
    mainPanel.add(leftPanel, BorderLayout.EAST);
    mainPanel.add(rightPanel, BorderLayout.WEST);
    createAndShowGui();
}
@Override
public Dimension getPreferredSize() {
    return PREF_SIZE;
}
private static void createAndShowGui() {
  UI frame = new UI();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setLocationByPlatform(true);
  frame.setVisible(true);
}
//public static void main(String[] args) {
  //SwingUtilities.invokeLater(new Runnable() {
    // public void run() {
      //  createAndShowGui();
   //  }
  //});
//} 
//public void paint(Graphics g)
//{

// }

public void actionPerformed(ActionEvent e)
{

}

}    

这是我的画布课

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;

public class MyCanvas extends Canvas 
{
private Elevator e;

int xPos =0;
int yPos=0;

public MyCanvas()
{
   setSize(600,600);
   repaint();
}


public void paint(Graphics g)
{
   g.setColor(Color.BLACK);
   g.fillRect(xPos,yPos,100, 100);


}

public void actionPerformed(ActionEvent e)
{
    repaint();
}
public void setElevator(Elevator ev)
{
    e = ev;
}
} 

这就是我想要完成的项目

4

3 回答 3

4

你覆盖了框架的paint方法......

public void paint(Graphics g)
{

}

但未能调用super.paint(g)....

的职责之一paint是绘制子组件...

更新

此外,由于约束已被丢弃,调用this.setLayout(new BorderLayout())会扰乱您的布局,这意味着布局管理器不知道应该去哪里

更新示例

我能够制作这个...

在此处输入图像描述

有了这个

public class Elevator02 {

    public static void main(String[] args) {
        new Elevator02();
    }

    public Elevator02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                UI ui = new UI();
                ui.setLocationRelativeTo(null);

            }
        });
    }

    public class UI extends JFrame implements ActionListener {

        ArrayList<Button> buttonList = new ArrayList();
        JPanel buttonPanel;

        public UI() {
            super("Elevators");
            setSize(200, 400);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);

            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new GridLayout(1, 3));

            buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(12, 1));

            add(mainPanel);

            for (int i = 0; i < 12; i++) {
                buttonList.add(new Button("" + i));
                buttonPanel.add(buttonList.get(i));
                buttonList.get(i).addActionListener(this);
            }
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainPanel.add(buttonPanel);
        }

//        public void paint(Graphics g) {
//        }
        public void actionPerformed(ActionEvent e) {
        }
    }
}
于 2012-10-25T02:09:24.080 回答
3

编辑:

之前(您提供的代码)

this.add(mainPanel); 

    for(int i=0; i<12; i++)
    {
        buttonList.add(new Button(""+i));
        buttonPanel.add(buttonList.get(i));
        buttonList.get(i).addActionListener(this);
    }
    this.setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainPanel.add(buttonPanel, BorderLayout.CENTER);

之后:(在添加组件之前设置框架的布局)

    this.setLayout(new BorderLayout()); // move this line
    this.add(mainPanel); 

    for(int i=0; i<12; i++)
    {
        buttonList.add(new Button(""+i));
        buttonPanel.add(buttonList.get(i));
        buttonList.get(i).addActionListener(this);
    }

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainPanel.add(buttonPanel, BorderLayout.CENTER);
于 2012-10-25T02:08:17.497 回答
2

建议:

  • 不要将 JFrame 的布局设置为 BorderLayout——它的 contentPane已经使用了BorderLayout。
  • 将组件添加到使用 GridLayout 的容器时,不要使用 BorderLayout 约束。
  • 使用 JButtons,而不是 Buttons(Swing 组件,而不是 AWT 组件)
  • 不要设置任何东西的大小。而是覆盖getPreferredSize()返回所需的维度。
  • 更好的是,让组件自己调整到最佳尺寸。
  • 您总是希望pack()在将所有组件添加到顶级窗口(此处为 JFrame)之后以及在调用setVisible(true).

例如,

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class UI extends JFrame {
   private static final Dimension PREF_SIZE = new Dimension(800, 800);
   private ArrayList<JButton> buttonList = new ArrayList<JButton>();
   private JPanel buttonPanel;

   public UI() {
      super("Elevators");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JPanel mainPanel = new JPanel();
      mainPanel.setLayout(new BorderLayout());

      buttonPanel = new JPanel();
      buttonPanel.setLayout(new GridLayout(12, 1));

      add(mainPanel);

      for (int i = 0; i < 12; i++) {
         buttonList.add(new JButton("" + i));
         JButton btn = buttonList.get(i);
         buttonPanel.add(btn);
      }
      mainPanel.add(buttonPanel, BorderLayout.CENTER);
   }

   @Override
   public Dimension getPreferredSize() {
      return PREF_SIZE;
   }

   private static void createAndShowGui() {
      UI frame = new UI();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

显示为,

按钮面板图像

编辑 2
考虑为整个 GUI 使用 BorderLayout,两个电梯图像可以放置在 BorderLayout.LINE_START 和 BorderLayout.LINE_END 位置。电梯 JPanel 可以使用空布局来允许电梯组件改变位置。中央 JPanel 也可以使用 BoxLayout.PAGE_AXIS(垂直)中的 BoxLayout。

于 2012-10-25T02:07:07.677 回答