-5

这是JPanel

public class DisplayBoard {

  public static void main (String[] args) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //The main panel
    JPanel main = new JPanel();
    main.setPreferredSize(new Dimension(600,800) );
    main.setLayout(new BorderLayout());

    //The title panel
    JPanel title = new JPanel();
    title.setPreferredSize(new Dimension(400, 120));
    title.setBackground(Color.YELLOW);
    JLabel test1 = new JLabel("Title goes here");
    title.add(test1);

    //The side bar panel
    JPanel sidebar = new JPanel();
    sidebar.setPreferredSize(new Dimension(200, 800));
    sidebar.add(AddSubtract);
    sidebar.setBackground(Color.GREEN);
    JLabel test2 = new JLabel("Sidebar goes here");
    sidebar.add(test2);

    //The panel that displays all the cards
    JPanel cardBoard = new JPanel();
    cardBoard.setPreferredSize(new Dimension(400,640) );

    //adding panels to the main panel
    main.add(cardBoard, BorderLayout.CENTER);
    main.add(title, BorderLayout.NORTH);
    main.add(sidebar, BorderLayout.WEST);

    frame.setContentPane(main);
    frame.pack();
    frame.setVisible(true);

  }
}

我想将这个类添加到侧边栏面板中

public class AddSubtract {

int Number = 0;
private JFrame Frame = new JFrame("Math");
private JPanel ContentPane = new JPanel();
private JButton Button1 = new JButton("Add");
private JButton Button2 = new JButton("Subtract");
private JLabel Num = new JLabel ("Number: " + Integer.toString (Number));

public AddSubtract() {

    Frame.setContentPane(ContentPane);
    ContentPane.add(Button1);
    ContentPane.add(Button2);
    ContentPane.add(Num);
    Button1.addActionListener(new Adding());
    Button2.addActionListener(new Subtracting());
}

public class Adding implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        Number++;
        Num.setText ("Number: " + Integer.toString (Number));
    }
}

public class Subtracting implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        Number--;
        Num.setText ("Number: " + Integer.toString (Number));
    }
}

public void launchFrame(){
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.pack();
    Frame.setVisible(true);
}

public static void main(String args[]){
    AddSubtract Test = new AddSubtract();
    Test.launchFrame();
    }
}

有人可以向我解释我该怎么做吗?我有一种感觉,这是行不通的,但我真的很想学习如何做到这一点。

4

1 回答 1

2

这肯定行不通。对于初学者,您有两种main()方法。其次,如果你想给你的 Frame 添加一个类,它应该从JComponent. 基本上,您的代码应如下所示:

public class MainFrame extends JFrame {
    public MainFrame() {
        this.add(new MainPanel())
        //insert all settings here.
    }
}

public class MainPanel extends JPanel {
    public MainPanel() {
        this.add(new AddSubtract());
        this.add(/*more panels*/)
    }
}

public class AddSubtract extends JPanel {
    public AddSubtract() {
        //add buttons and stuff here
    }
}

并且变量不以大写字母开头。

编辑:当你有一些 JFrame 时,通常最好有一个main()只有一行的方法:

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

只需在构造函数中设置设置和配置JFrame

于 2012-12-17T14:57:31.730 回答