提问人:Thend 提问时间:1/7/2022 最后编辑:James ZThend 更新时间:1/7/2022 访问量:176
BorderLayout 与 FlowLayout 的结合:JTextArea 破坏了 GUI (Java Swing)
BorderLayout combined with FlowLayout: JTextArea ruins GUI (Java Swing)
问:
我将 BorderLayout 与 FlowLayout 结合使用,这在设置 BorderLayout.NORTH 时非常有效。但是,在CENTER区域中,我想添加一个JTextArea(打印出控制台),但是当我创建面板时,添加JTextArea,在将面板添加到BorderLayout.CENTER后,什么都没有出现并变成灰色。我尝试了几种组合和技巧,也检查了几个论坛帖子,但没有运气。这是我非常简化的代码(应该完美运行,如果您删除存在错误的注释,我已经评论了有问题的部分):
public static void main(String[] args) {
blogGUI();
}
public static void blogGUI() {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(700, 500);
frame.setLayout(new BorderLayout()); //a frame BorderLayout elrendezésü
JPanel panel1 = new JPanel();
//panel.setBounds(0,0,700,500);
panel1.setBackground(new java.awt.Color(255,255,255));
panel1.setVisible(true);
panel1.setSize(new Dimension(140, 30));
JPanel panel2 = new JPanel();
//panel.setBounds(0,0,700,500);
panel2.setBackground(new java.awt.Color(255,255,255));
panel2.setVisible(true);
panel2.setSize(new Dimension(140, 30));
JButton btNewEntry = new JButton("New Post");
JButton btModifyEntry = new JButton("Modify");
JButton btDeleteEntry = new JButton("Delete");
JButton btShowEntries = new JButton("List");
JButton btExit = new JButton("Exit");
JLabel lbFile = new JLabel("Open Blog:");
JLabel lbFilePath = new JLabel("Nothing selected...");
JButton btFileOpen = new JButton("Open");
btNewEntry.setPreferredSize(new Dimension(100,30));
btModifyEntry.setPreferredSize(new Dimension(100,30));
btDeleteEntry.setPreferredSize(new Dimension(100,30));
btShowEntries.setPreferredSize(new Dimension(100,30));
btExit.setPreferredSize(new Dimension(100,30));
lbFile.setPreferredSize(new Dimension(100,30));
lbFilePath.setPreferredSize(new Dimension(310,30));
btFileOpen.setPreferredSize(new Dimension(100,30));
panel1.add(btNewEntry);
panel1.add(btModifyEntry);
panel1.add(btDeleteEntry);
panel1.add(btShowEntries);
panel1.add(btExit);
panel2.add(lbFile);
panel2.add(btFileOpen);
panel2.add(lbFilePath);
JPanel cpanelNorth = new JPanel();
cpanelNorth.setBackground(new java.awt.Color(135,206,250));
cpanelNorth.setLayout(new FlowLayout());
cpanelNorth.setPreferredSize(new Dimension(500, 95));
cpanelNorth.add(panel1);
cpanelNorth.add(panel2);
frame.add(cpanelNorth, BorderLayout.NORTH);
/*Something wrong here! From this point, if this uncommented ruins the gui.*/
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
//panel.setBounds(0,0,700,500);
panel3.setBackground(new java.awt.Color(255,255,255));
//panel3.setVisible(true);
//panel3.setPreferredSize(new Dimension(30, 30));
JTextArea textArea = new JTextArea("Welcome to...!");
//textArea.setPreferredSize(new Dimension(50,50));
textArea.setBackground(new java.awt.Color(255,0,0));
panel3.add(textArea);
frame.add(panel3, BorderLayout.CENTER);
答:
0赞
Thend
1/7/2022
#1
嗨,安德鲁·汤普森(Andrew Thompson)和user16320675,感谢您的快速帮助,这解决了我的问题。如何进一步提高代码质量?我只需要在 ,我删除的所有其他地方都放了一个(没有这个,窗口就会缩小到零)setPrefferedSize()
JTextArea
- 我减少了我的代码
- 图片:https://ibb.co/Xj7tYKf
- 我已删除
allsetPrefferedSize()
- 我放在最后,也放在最后
frame.setVisible(true)
frame.pack()
这是我的代码:
package bloggui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Start {
public static void main(String[] args) {
blogGUI();
}
public static void blogGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setBackground(new java.awt.Color(255,255,255));
JPanel panel2 = new JPanel();
panel2.setBackground(new java.awt.Color(255,255,255));
JButton btNewEntry = new JButton("New Post");
JButton btModifyEntry = new JButton("Modify");
JButton btDeleteEntry = new JButton("Delete");
JButton btShowEntries = new JButton("List");
JButton btExit = new JButton("Exit");
JLabel lbFile = new JLabel("Open Blog:");
JLabel lbFilePath = new JLabel("Nothing selected...");
JButton btFileOpen = new JButton("Open");
panel1.add(btNewEntry);
panel1.add(btModifyEntry);
panel1.add(btDeleteEntry);
panel1.add(btShowEntries);
panel1.add(btExit);
panel2.add(lbFile);
panel2.add(btFileOpen);
panel2.add(lbFilePath);
JPanel cpanelNorth = new JPanel();
cpanelNorth.setBackground(new java.awt.Color(135,206,250));
cpanelNorth.setLayout(new FlowLayout());
cpanelNorth.add(panel1);
cpanelNorth.add(panel2);
frame.add(cpanelNorth, BorderLayout.NORTH);
frame.pack();
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.setBackground(new java.awt.Color(135,206,250));
JTextArea textArea = new JTextArea("Welcome to...!");
textArea.setBackground(new java.awt.Color(255,255,255));
textArea.setPreferredSize(new Dimension(620, 400)); //Must not use setPreferredSize() [I must use it only once here!]
panel3.add(textArea);
frame.add(panel3, BorderLayout.CENTER);
frame.pack(); //Must be at the end
frame.setVisible(true); //Must be at the end
}
}
评论
1赞
c0der
1/7/2022
如何进一步提高代码质量?这是一个答案还是一个问题?对于 codereview.stackexchange.com 中的代码审查帖子
2赞
Andrew Thompson
1/7/2022
“我只需要在 JTextArea
中放置一个 setPrefferedSize()
”那里也没有必要。 应该更像 其中 & 都是整数值。然后,文本区域将根据行数和列数以及它使用的字体的大小返回首选大小。new JTextArea("Welcome to...!");
new JTextArea("Welcome to...!", rows, columns);
rows
columns
评论
frame.setVisible(true);
应该是最后,紧跟在frame.pack();
之后。setPreferredSize
frame.add(panel3, BorderLayout.CENTER);
panel3
FlowLayout
FlowLayout