【发布时间】:2017-08-04 02:34:04
【问题描述】:
我正在尝试使用 swing 编写一个程序,以便左侧有一个 JList,右侧有五个 JButton。所以我为一个 JButton 编写了代码,但我无法调整它的大小或移动它。任何帮助,将不胜感激。非常感谢!!这是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GenericFrame {
private JFrame mainFrame;
private JPanel controlPanel;
public GenericFrame(){
prepareGUI();
}
private void prepareGUI(){
mainFrame = new JFrame("Generic Frame");
mainFrame.setSize(800,400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new FlowLayout());
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.setSize(800,400);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
public void showButtons(){
JButton showButton = new JButton("Show");
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("hello");
}
});
showButton.setLayout(new BorderLayout());
showButton.setLocation(0, 200);
JButton viewButton = new JButton("view");
viewButton.setLocation(showButton.getX(), showButton.getY() + 100);
controlPanel.add(showButton);
controlPanel.add(viewButton);
}
public static void main(String[] args){
GenericFrame swingControlDemo = new GenericFrame();
swingControlDemo.showButtons();
}
}
为了记录,我正在使用 IntelliJ。我在具有不同操作系统的多台机器上尝试了此操作,但此错误仍然存在。请帮帮我。
【问题讨论】:
标签: java swing intellij-idea