Java – GUI does not display as expected
•
Java
I tried to draw the GUI as shown in the figure, but somehow I couldn't put the object in the correct position (I guess the problem is the layout) textarea is assumed to be in the middle But it didn't show at all
package Chapter22Collections;
import javax.swing.*;
import java.awt.*;
public class Exercise226 extends JFrame {
private JButton jbSort;
private JButton jbReverse;
private JButton jbAdd;
private JButton jbShuffle;
private JLabel jlAddnum;
private JTextArea jTextDisplay;
private JTextField jTextAdd;
public Exercise226() {
jbSort = new JButton("Sort");
jbReverse = new JButton("Reverse");
jbShuffle = new JButton("Shuffle");
jbAdd = new JButton("Add");
jlAddnum = new JLabel("Add number here: ");
jTextDisplay = new JTextArea();
jTextAdd = new JTextField(8);
setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(1,3));
p1.add(jlAddnum);
p1.add(jTextAdd);
p1.add(jbAdd);
JPanel p2 = new JPanel(new GridLayout(1,3));
p2.add(jbSort);
p2.add(jbReverse);
p2.add(jbShuffle);
add(p1,BorderLayout.NORTH);
add(jTextDisplay,BorderLayout.CENTER);
add(p2,BorderLayout.soUTH);
}
public static void main(String... args) {
Exercise226 gui = new Exercise226();
gui.setTitle("Numbers");
gui.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300,200);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
}
}
Solution
Jtextarea is actually what you expect, but there is no outline border Components are usually placed in JScrollPane, which will have this effect:
add(new JScrollPane(jTextDisplay),BorderLayout.CENTER);
Or just
add(new JScrollPane(jTextDisplay));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
