为什么Jtextarea没有显示在此代码中? 我尝试使用gridbaglayout在jpanel中添加Jtextarea。 框架打开正常,但没有Jtextarea。 我无法确定问题。有人会帮助我。
import javax.swing.*;
import java.awt.*;
public class ServerTest{
//object declaration
JFrame f;
JPanel p;
JTextArea ta;
JTextField tf;
JButton b1,b2;
GridBagConstraints gbc;
//constructor
public ServerTest(){
//instantiation
f=new JFrame("Server");
p=new JPanel();
p.setBackground(Color.green);
ta=new JTextArea("Hello");
tf=new JTextField();
b1=new JButton("EMO");
b2=new JButton("VOICE");
gbc=new GridBagConstraints();
//end of instantiation
//frame task
f.setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.setLayout(new GridBagLayout());
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=3;
gbc.gridheight=5;
p.add(ta,gbc);
f.add(p);
f.pack();
f.setVisible(true);
//end of frame task
}
//Main method
public static void main(String []args){
ServerTest st = new ServerTest();
}
}
答案 0 :(得分:1)
简单的答案,有......
这是证据......
如果你使用JTextArea(int, int)
构造函数并使用JScrollPane
,你可能会有更好的运气,例如......
public ServerTest() {
//...
ta = new JTextArea(10, 20);
//...
p.add(new JScrollPane(ta), gbc);
}
答案 1 :(得分:0)
您应该设置if (driver instanceof JavascriptExecutor)
{
((JavascriptExecutor)driver).executeScript("document.getElementById('company_stage').classList.remove('theclassnamewhichhidestheelement');");
}
else
{
throw new IllegalStateException("This driver does not support JavaScript!");
}
的一些属性来初始化布局的背景网格:
GridBagLayout
将整个框架的布局从columnWeights
columnWidths
rowWeights
rowHeights
更改为FlowLayout
也不错:
BorderLayout
你改变你的代码就像这个样本一样,它会起作用:
f.setLayout(new BorderLayout());
使用//frame task
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout bagLayout = new GridBagLayout();
bagLayout.columnWeights = new double[]{1.0,1.0,1.0,1.0,1.0};
bagLayout.columnWidths = new int[]{25,25,25,25,25};
bagLayout.rowWeights = new double[]{1.0,1.0,1.0,1.0,1.0};
bagLayout.rowHeights = new int[]{25,25,25,25,25};
p.setLayout(bagLayout);
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=3;
gbc.gridheight=5;
gbc.fill = GridBagConstraints.BOTH;
时,GridBagConstraints#fill
属性在调整组件大小方面也有重要作用。
如果你想看一些详细的例子: SWING - GridBagLayout Class
祝你好运