日期:2014-05-20 浏览次数:20757 次
public class Main extends JFrame (){ private JPanel contentPane; private JButton btnClick; private JTextField textField; public static void main(String[] args) { new Main().setVisible(true); } public Main() { initGUI(); } private void initGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); btnClick = new JButton("click"); contentPane.add(btnClick, BorderLayout.NORTH); textField = new JTextField(); contentPane.add(textField, BorderLayout.CENTER); } }
public class RequestValueDialog extends JDialog(){ public RequestValueDialog() { setBounds(100, 100, 450, 300); getContentPane().setLayout(new BorderLayout()); contentPanel.setLayout(new FlowLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); { textField = new JTextField(); contentPanel.add(textField); textField.setColumns(10); } { textField_1 = new JTextField(); textField_1.setColumns(10); contentPanel.add(textField_1); } { textField_2 = new JTextField(); textField_2.setColumns(10); contentPanel.add(textField_2); } { JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH); { JButton okButton = new JButton("OK"); okButton.setActionCommand("OK"); buttonPane.add(okButton); getRootPane().setDefaultButton(okButton); } { JButton cancelButton = new JButton("Cancel"); cancelButton.setActionCommand("Cancel"); buttonPane.add(cancelButton); } } } }
import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Main extends JFrame implements ActionListener { private JPanel contentPane; private JButton btnClick; public JTextField getTextField() { return textField; } public void setTextField(JTextField textField) { this.textField = textField; } private JTextField textField; public static void main(String[] args) { new Main().setVisible(true); } public Main() { initGUI(); } private void initGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); btnClick = new JButton("click"); btnClick.addActionListener(this); contentPane.add(btnClick, BorderLayout.NORTH); textField = new JTextField(); contentPane.add(textField, BorderLayout.CENTER); } @Override public void actionPerformed(ActionEvent e) { //To change body of implemented methods use File | Settings | File Templates. if(e.getSource()==btnClick){ RequestValueDialog dialog=new RequestValueDialog(this); dialog.setVisible(true); } } }