问题和问题在第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,无论其他尺寸如何,窗户的大小都会改变。
谢谢! :)
答案 0 :(得分:2)
我决定不再对接受的答案进行进一步的批评,而是展示我的意思('把你的代码放在嘴巴的位置')会更好。有关更多详细信息,请参阅源代码中的注释。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class MainFrame extends JFrame {
JButton btnTest;
JPanel btnContainer;
// ---------------------------------------------------------------
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame frame = new MainFrame();
frame.pack();
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
public MainFrame() {
this.setTitle("Exercise 1");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setLocation(300, 200); BETTER TO..
this.setLocationByPlatform(true);
// THESE NUMBERS ARE NOT MUCH BETTER THAN 'MAGIC NUMBERS'
//this.getContentPane().setPreferredSize(new Dimension(400, 150));
// DON'T DO THIS - IT WILL CAUSE MORE PROBLEMS THAN IT FIXES
//this.getContentPane().setLayout(null);
this.initContent();
}
// ---------------------------------------------------------------
private final JButton btnCombine = new JButton();
private void initContent() {
btnTest = new JButton("Change Size");
//btnTest.setSize(100, 25); DON'T GUESS COMPONENT SIZES!
//btnTest.setLocation(150, 100); ADD A BORDER INSTEAD!
// WE ACTUALLY ADD THE BORDER TO A CONTAINER THAT HOLDS THE BUTTON.
// THIS WAY IT RETAINS THE USUAL BORDERS ON FOCUS, PRESS ETC.
btnContainer = new JPanel(new GridLayout());
btnContainer.add(btnTest);
btnContainer.setBorder(new EmptyBorder(100,150,100,150));
btnTest.addActionListener(action);
add(btnContainer);
}
// ---------------------------------------------------------------
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;
btnContainer.setBorder(new EmptyBorder(30,20,30,20));
MainFrame.this.pack();
}
}
}
}
第二点在上述来源中实施。第一个留给读者练习。
答案 1 :(得分:0)
更改行
MainFrame.getContentPane().setPreferredSize(new Dimension(100, 200));
到
setSize(new Dimension(100, 200));
您需要在外部类对象(主框架)上调用setSize
方法。