问题
I have an applet (and this is an SSCCE):
package tutoringcalculator;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;
public class TutoringCalculator extends JApplet {
private JPanel _root;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Tutoring Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new TutoringCalculator();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}
});
}
private JPanel swingContainer;
@Override
public void init() {
swingContainer = new JPanel(new BorderLayout());
add(swingContainer, BorderLayout.CENTER);
createScene();
setSize(600, 400);
}
private void createScene() {
JLabel lbl;
JTextField txt;
_root = new JPanel(new FlowLayout());
// build the session minutes
lbl = new JLabel();
lbl.setText("Session Minutes:");
_root.add(lbl);
txt = new JTextField();
_root.add(txt);
swingContainer.add(_root);
}
}
and I'd like to set the size to 600x400 - but I've issued setSize(600, 400) on applet, frame, swingContainer, and _root and nothing changes. I get a screen that looks like this:
回答1:
As mentioned, an applet typically gets a size (forced upon it) by the HTML. A free floating JFrame is different. Instead it has components inside which suggest a size, which is then calculated at time of pack(). E.G. after setting a size in the constructor of the JTextField, this is what we see:
Now, I expect you are going to be adding a whole lot more components to this before it's done, and they will make the size bigger as well, but there are also some other tips for making the content larger. Borders & layout padding. Here is what it looks like with some of that.
So, what it comes down to is either a) Add a whole bunch of components that will make a GUI bigger, and/or b) add white space as defined in the layout constructors & borders.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TutoringCalculator {
private JPanel _root;
private JPanel swingContainer;
private JLabel lbl;
private JTextField txt;
TutoringCalculator() {
swingContainer = new JPanel(new BorderLayout());
// set an empty border to it
swingContainer.setBorder(new EmptyBorder(20,20,20,20));
// suggest a spacing of 20px between components
_root = new JPanel(new FlowLayout( FlowLayout.CENTER, 20, 20));
swingContainer.add(_root);
// add a line border so we can see the bounds of this container
_root.setBorder(new LineBorder(Color.BLACK, 1));
// this has a size as needed by the content
lbl = new JLabel("Session Minutes:");
_root.add(lbl);
// Suggest a size (in characters - 10) for the text field
txt = new JTextField(10);
_root.add(txt);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Tutoring Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TutoringCalculator calc = new TutoringCalculator();
frame.setContentPane(calc.swingContainer);
frame.pack();
// much better!
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
}
来源:https://stackoverflow.com/questions/16582407/how-can-i-set-the-size-of-this-gui