2012-11-26 51 views
4

我想要做的是在框架内组织五个独立的JPanel。以下是输出应该是这样的:顶部将会有一个面板。垂直分割空间的顶部面板下方的两个面板,以及另外两个水平分割剩余空间的面板。什么是在JFrame中组织多个JPanel的好方法?

enter image description here

我无法弄清楚如何组织面板如同上述,我认为这是因为我只是不知道正确的语法。所以任何帮助或建议非常感谢这里是我迄今为止的代码。

import java.lang.String.*; 
import java.lang.Exception.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Display extends JFrame implements ActionListener{ 
// instance variables 
private static final int FRAME_WIDTH = 400; 
private static final int FRAME_HEIGHT = 350; 

private static final int FRAME_X_ORIGIN = 150; 
private static final int FRAME_Y_ORIGIN = 150; 

private static final int BUTTON_WIDTH = 90; 
private static final int BUTTON_HEIGHT = 30; 

private JButton readFile; 
private JButton exit; 
private JButton stats; 
private JButton blank; 

private JCheckBox avgHomeworkScore; 
private JCheckBox avgTestScore; 
private JCheckBox sdHomeworkScore; 
private JCheckBox sdTestScore; 

private JRadioButton buttonOne; 
private JRadioButton buttonTwo; 
private JRadioButton buttonThree; 
private JRadioButton buttonFour; 

private JPanel header; 
private JPanel statistics; 
private JPanel courses; 
private JPanel display; 
private JPanel action; 

public static void main(String[] args){ 
    Display frame = new Display(); 
    frame.setVisible(true); 
} 


public Display(){ 

    Container contentPane; 


    //Set the frame properties 
    setSize   (FRAME_WIDTH, FRAME_HEIGHT); 
    setResizable (false); 
    setTitle  ("CSCE155A Course Offerings Viewer"); 
    setLocation  (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); 

    contentPane = getContentPane(); 
    contentPane.setLayout(new GridLayout()); 
    contentPane.setBackground(Color.white); 

    //header 





    //Create and Place the Buttons on the frame 
    readFile = new JButton("Read File"); 
    readFile.setBounds(4, 285, BUTTON_WIDTH, BUTTON_HEIGHT); 


    exit = new JButton("Exit"); 
    exit.setBounds(100, 285, BUTTON_WIDTH, BUTTON_HEIGHT); 


    stats = new JButton("Stats"); 
    stats.setBounds(195, 285, BUTTON_WIDTH, BUTTON_HEIGHT); 


    blank = new JButton("Clear"); 
    blank.setBounds(290, 285, BUTTON_WIDTH, BUTTON_HEIGHT); 


    action = new JPanel(new FlowLayout()); 
    action.setBackground(Color.blue); 
    action.add(readFile); 
    action.add(exit); 
    action.add(stats); 
    action.add(blank); 
    contentPane.add(action); 

    //Register this frame as an Action listener of the buttons 
    readFile.addActionListener(this); 
    exit.addActionListener(this); 
    stats.addActionListener(this); 
    blank.addActionListener(this); 

    //Create and Place the checkboxes on the frame 
    avgHomeworkScore = new JCheckBox(); 
    avgHomeworkScore.setMnemonic(KeyEvent.VK_H); 
    contentPane.add(avgHomeworkScore); 
    avgHomeworkScore.setSelected(true); 

    avgTestScore = new JCheckBox(); 
    avgTestScore.setMnemonic(KeyEvent.VK_T); 
    avgTestScore.setSelected(true); 

    sdHomeworkScore = new JCheckBox(); 
    sdHomeworkScore.setMnemonic(KeyEvent.VK_S); 
    sdHomeworkScore.setSelected(true); 

    sdTestScore = new JCheckBox(); 
    sdTestScore.setMnemonic(KeyEvent.VK_D); 
    sdTestScore.setSelected(true); 

    statistics = new JPanel(new GridLayout(0,1)); 
    contentPane.add(statistics); 
    statistics.add(avgHomeworkScore); 
    statistics.add(avgTestScore); 
    statistics.add(sdHomeworkScore); 
    statistics.add(sdTestScore); 

    avgHomeworkScore.addActionListener(this); 
    avgTestScore.addActionListener(this); 
    sdHomeworkScore.addActionListener(this); 
    sdTestScore.addActionListener(this); 



    //create the radio buttons 
    buttonOne = new JRadioButton(); 
    buttonOne.setMnemonic(KeyEvent.VK_1); 
    buttonOne.setSelected(true); 

    buttonTwo = new JRadioButton(); 
    buttonTwo.setMnemonic(KeyEvent.VK_2); 

    buttonThree = new JRadioButton(); 
    buttonThree.setMnemonic(KeyEvent.VK_3); 

    buttonFour = new JRadioButton(); 
    buttonFour.setMnemonic(KeyEvent.VK_4); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(buttonOne); 
    group.add(buttonTwo); 
    group.add(buttonThree); 
    group.add(buttonFour); 

    buttonOne.addActionListener(this); 
    buttonTwo.addActionListener(this); 
    buttonThree.addActionListener(this); 
    buttonFour.addActionListener(this); 

    courses = new JPanel(new GridLayout(0,1)); 
    courses.setBackground(Color.blue); 
    courses.add(buttonOne); 
    courses.add(buttonTwo); 
    courses.add(buttonThree); 
    courses.add(buttonFour); 
    contentPane.add(courses); 

    //Exit program when the viewer is closed 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
} 
+1

您可以使用ASCII-art来展示您希望如何排列面板。 – David

回答

7

使用布局管理器。切勿设置面板和其他组件的边界,大小和位置。这是布局经理的工作。版面管理器在the Swing tutorial中详细解释(如其他所有,顺便说一句)。

例如,您可以为主面板使用BorderLayout,为底部面板使用BorderLayout。

+0

我最初试图用GridLayout来组织它,但它不起作用。我发布了它应该看起来像上面的内容。布局经理能做到吗? – user1854263

+0

您可以在北部使用带有CSC ...标签的BorderLayout,中部的请选择面板,东部的课程产品以及包含南部两个其他面板的面板。这个南方面板本身会使用带有中心统计数据的BorderLayout和南方的按钮。 GridBagLayout,GroupLayout等也有其他可能性。 –

相关问题