问题和疑问在第 50-51 行:
package exercise1;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MainFrame extends JFrame {
// -------------------------------------------------------------------------
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.pack();
frame.setVisible(true);
}
public MainFrame() {
this.setTitle("Exercise 1");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(300, 200);
this.getContentPane().setPreferredSize(new Dimension(400, 150));
this.getContentPane().setLayout(null);
this.initContent();
}
// -------------------------------------------------------------------------
private final JButton btnCombine = new JButton();
private void initContent() {
this.add(btnTest);
btnTest.setText("Change Size");
btnTest.setSize(100, 25);
btnTest.setLocation(150, 100);
btnTest.addActionListener(action);
}
// -------------------------------------------------------------------------
private final Controller action = new Controller();
private class Controller implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnTest) {
//CHANGE THE WINDOWS SIZE HERE;
MainFrame.getContentPane().setPreferredSize(new Dimension(100, 200));
}
}
}
}
如果我有这样的代码,如何更改窗口的大小?
有一个条件:我想在单击按钮时更改它的大小。在这个例子中:我点击按钮 btnTest 并且窗口的大小改变为不管其他大小。
谢谢!:)