組合

2015-11-20 24 views
1

大家好,我想我的班 只有一個框架結合起來,並得到它。現在我有兩節課,我不知道如何分組。 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()); 
    } 
} 

在PROGRAMM結束時,我看到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); 





     } 

    }); 

回答

4

如果你不希望看到2個JFrames,則不會產生2個JFrames。爲什麼不使用上面的所有類而不使用JFrames,然後在主要方法中將JPanel添加到在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); 
     } 

    }); 
}