类的组合

时间:2015-11-20 22:19:12

标签: java swing jpanel

  

大家好,我想把我的课程结合起来   只有一帧。现在我有2个课程,我不知道如何分组。        JSlider。

    public class JSliderExample extends JFrame {
    JSlider jsHorizontal;
    JTextField jtf1;

    public JSliderExample() {

        jsHorizontal = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);


    jtf1 = new JTextField(15);
        jtf1.setEditable(false);
        jtf1.setText("Horizontal value is " + jsHorizontal.getValue());

        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.add(jsHorizontal);
        panel.setBackground(Color.WHITE);
        panel.add(jtf1);
        panel.setBackground(Color.WHITE);


        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(panel, BorderLayout.SOUTH);


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(300, 400, 400, 300);
        setVisible(true);
        setBackground(Color.WHITE);
    }

    class JSliderHandler implements ChangeListener {
        public void stateChanged(ChangeEvent ce) {
            jtf1.setText("value is " + jsHorizontal.getValue());

        }
    }
  

还有我的按钮

    public void createGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());


    JButton button2 = new JButton("PLAY");
    button2.setActionCommand("Button PLAY was pressed!");
            panel.add(button2);



    textField = new JTextField();
    textField.setColumns(23);
    panel.add(textField);

    ActionListener actionListener = new TestActionListener();

    button1.addActionListener(actionListener);
    button2.addActionListener(actionListener);

    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textField.setText(e.getActionCommand());
        }
    });

    getContentPane().add(panel);
    setPreferredSize(new Dimension(320, 100));
}

  public class TestActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        textField.setText(e.getActionCommand());
    }
}
  

在程序结束时,我看到2个由2个类组成的帧。

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            TestFrame frame = new TestFrame();
            frame.pack();
          JSliderExample frame1 = new JSliderExample();

            frame.setLocationRelativeTo(null);

            frame.setVisible(true);





        }

    });

1 个答案:

答案 0 :(得分:4)

如果您不想看到2个JFrame,那么就不要创建2个JFrame。为什么不使用上面的所有类而不是JFrame创建JPanels,然后在main方法中,将JPanels添加到main中创建的JFrame。简单。

因此,例如,不要让JSliderExample扩展JFrame,而是将其名称更改为SliderPanel,并将其扩展为JPanel,同样使用JButton程序。然后你的主要方法看起来像:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            // your JSlider example class **that extends JPanel**
            SliderPanel sliderPanel = new SliderPanel();

            // your JButton example class **that extends JPanel**
            ButtonPanel buttonPanel = new ButtonPanel():

            JFrame frame = new JFrame("My GUI");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(sliderPanel, BorderLayout.PAGE_START);
            frame.add(buttonPanel, BorderLayout.CENTER);
            frame.pack();
            frame.setLocationRelativeTo(null); // center GUI if you want
            frame.setVisible(true);
        }

    });
}