java中不出现文本框怎么回事?
2015-05-13
展开全部
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TextFile extends JFrame {
private Container c;
private JTextField jtf;//文本框
private JButton jb;//按钮
private JComboBox jc;//下拉框
private JTextArea jta;//文本区
private JScrollPane jsp;
public TextFile() {
this.setTitle("页面展示");
c = this.getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.setLayout(gridbag);
jtf = new JTextField();
jb = new JButton("BUTTON开始");
String[] city = {"下拉框列表", "中", "美", "英"};
jc = new JComboBox(city);
jta = new JTextArea();
jta.setRows(10);//设置文本区列数
jsp = new JScrollPane();
gbc.anchor = GridBagConstraints.NORTH; // 当组件没有空间大时,使组件处在北部
gbc.fill = GridBagConstraints.HORIZONTAL;;
gbc.gridx = 0;//横坐标
gbc.gridy = 0;//纵坐标
gbc.gridwidth = 2;//横占几个网格
jtf.setText("这里是文本框,显示一些东西");
gridbag.setConstraints(jtf, gbc);
c.add(jtf);
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(10, 0, 0, 10); // 组件彼此的间距
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.ipadx = 0; // 组件内部填充空间,即给组件的最小宽度添加多大的空间
gbc.ipady = -100; // 组件内部填充空间,即给组件的最小高度添加多大的空间
gridbag.setConstraints(jc, gbc);
c.add(jc);
gbc.insets = new Insets(10, 0, 10, 0); // 组件彼此的间距
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.ipadx = 0; // 组件内部填充空间,即给组件的最小宽度添加多大的空间
gbc.ipady = 0; // 组件内部填充空间,即给组件的最小高度添加多大的空间
gridbag.setConstraints(jb, gbc);
c.add(jb);
jta.setText("这里输出内容,可以往下拉");
gbc.insets = new Insets(10, 0, 0, 0); // 组件彼此的间距
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = GridBagConstraints.REMAINDER;
jsp.setViewportView(jta);
gridbag.setConstraints(jsp, gbc);
c.add(jsp);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new TextFile();
}
}
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TextFile extends JFrame {
private Container c;
private JTextField jtf;//文本框
private JButton jb;//按钮
private JComboBox jc;//下拉框
private JTextArea jta;//文本区
private JScrollPane jsp;
public TextFile() {
this.setTitle("页面展示");
c = this.getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.setLayout(gridbag);
jtf = new JTextField();
jb = new JButton("BUTTON开始");
String[] city = {"下拉框列表", "中", "美", "英"};
jc = new JComboBox(city);
jta = new JTextArea();
jta.setRows(10);//设置文本区列数
jsp = new JScrollPane();
gbc.anchor = GridBagConstraints.NORTH; // 当组件没有空间大时,使组件处在北部
gbc.fill = GridBagConstraints.HORIZONTAL;;
gbc.gridx = 0;//横坐标
gbc.gridy = 0;//纵坐标
gbc.gridwidth = 2;//横占几个网格
jtf.setText("这里是文本框,显示一些东西");
gridbag.setConstraints(jtf, gbc);
c.add(jtf);
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(10, 0, 0, 10); // 组件彼此的间距
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.ipadx = 0; // 组件内部填充空间,即给组件的最小宽度添加多大的空间
gbc.ipady = -100; // 组件内部填充空间,即给组件的最小高度添加多大的空间
gridbag.setConstraints(jc, gbc);
c.add(jc);
gbc.insets = new Insets(10, 0, 10, 0); // 组件彼此的间距
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.ipadx = 0; // 组件内部填充空间,即给组件的最小宽度添加多大的空间
gbc.ipady = 0; // 组件内部填充空间,即给组件的最小高度添加多大的空间
gridbag.setConstraints(jb, gbc);
c.add(jb);
jta.setText("这里输出内容,可以往下拉");
gbc.insets = new Insets(10, 0, 0, 0); // 组件彼此的间距
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = GridBagConstraints.REMAINDER;
jsp.setViewportView(jta);
gridbag.setConstraints(jsp, gbc);
c.add(jsp);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new TextFile();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询