java中的JTextField在GridLayout中怎么设置大小?
importjava.awt.*;importjavax.swing.*;publicclassL5_13_L5_14用户登录界面extendsJFrame{JLabel...
import java.awt.*;import javax.swing.*;public class L5_13_L5_14用户登录界面 extends JFrame{JLabel[] biaoqian = new JLabel[10];JPanel[] jp = new JPanel[10];JButton[] button = new JButton[10];JCheckBox[] fxk = new JCheckBox[10];JTextField wbk;JPasswordField mmk;public static void main(String[] args){L5_13_L5_14用户登录界面 l = new L5_13_L5_14用户登录界面();}L5_13_L5_14用户登录界面(){biaoqian[0] = new JLabel(new ImageIcon("image/qqq.png"));biaoqian[0].setPreferredSize(new Dimension(0, 50));//这个是设置高度的大小的。jp[0] = new JPanel(new GridLayout(3, 3));jp[1] = new JPanel();biaoqian[1] = new JLabel("用户名", JLabel.CENTER);biaoqian[2] = new JLabel("密 码", JLabel.CENTER);wbk = new JTextField(10);mmk = new JPasswordField();button[0] = new JButton(new ImageIcon("image/baidu.png"));button[0] = new JButton("按钮");biaoqian[3] = new JLabel("忘记密码");biaoqian[3].setFont(new Font("宋体", Font.PLAIN, 16));biaoqian[3].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));fxk[0] = new JCheckBox("隐身登录");fxk[1] = new JCheckBox("记住梦想");biaoqian[4] = new JLabel("<html><a href = 'www.qq.com'>申请密码保护</a>");biaoqian[4].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));button[1] = new JButton("登录");button[2] = new JButton("取消");button[3] = new JButton("向导");jp[0].add(biaoqian[1]); jp[0].add(wbk); jp[0].add(button[0]);jp[0].add(biaoqian[2]); jp[0].add(mmk); jp[0].add(biaoqian[3]);jp[0].add(fxk[0]); jp[0].add(fxk[1]); jp[0].add(biaoqian[4]);jp[1].add(button[1]); jp[1].add(button[2]); jp[1].add(button[3]); jp[1].setPreferredSize(new Dimension(0, 50));this.add(biaoqian[0], BorderLayout.NORTH);this.add(jp[0]);this.add(jp[1], BorderLayout.SOUTH);this.setSize(320, 255);this.setLocation(800, 400);//this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}这个是代码,刚才发的图片是运行图,我想问问大神们怎么把文本框(输入账号和密码的)变得矮一点,我上网搜了好多方法都不管用,可能是不适合我的这种情况。我的文本框是放在GridLayout里的。GrilLayout的格式为GridLayout(3 , 3);我知道可以用GridLayout(3, 3, 15, 15);来调整各个组件之间的间距。但是我想实现的是单独的把JTextField文本框变小。大神们看看能不能帮我解决一下。谢谢了!
展开
2个回答
展开全部
性别单选按键组的问题,你性别单选按钮组是用的JPanel装的两个JRadioButton吧,把这个JPanel的Layout设为 new FlowLayout(FlowLayout.CENTER, 0, 0)就OK了,见下面代码:
Java代码
package org.zergle.test.swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class ReaderAddIFrame extends JFrame {
private JLabel lblName = new JLabel("姓名");
private JTextField txtName = new JTextField();
private JLabel lblSex = new JLabel("性别");
private JRadioButton radMale = new JRadioButton("男");
private JRadioButton radFemale = new JRadioButton("女");
private JLabel lblAge = new JLabel("年龄");
private JTextField txtAge = new JTextField();
private JLabel lblCareer = new JLabel("职业");
private JTextField txtCareer = new JTextField();
private JPanel pnl1 = new JPanel();
private JPanel pnl1_1 = new JPanel(new GridLayout(2, 4));
private JPanel pnlSexGroup = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
public ReaderAddIFrame() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.pnlSexGroup.add(this.radMale);
this.pnlSexGroup.add(this.radFemale);
this.pnl1_1.add(this.lblName);
this.pnl1_1.add(this.txtName);
this.pnl1_1.add(this.lblSex);
this.pnl1_1.add(this.pnlSexGroup);
this.pnl1_1.add(this.lblAge);
this.pnl1_1.add(this.txtAge);
this.pnl1_1.add(this.lblCareer);
this.pnl1_1.add(this.txtCareer);
this.pnl1.add(this.pnl1_1);
this.add(this.pnl1, BorderLayout.CENTER);
this.setSize(600, 400);
this.setVisible(true);
}
public static void main(String[] args) {
new ReaderAddIFrame();
}
}
Java代码
package org.zergle.test.swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class ReaderAddIFrame extends JFrame {
private JLabel lblName = new JLabel("姓名");
private JTextField txtName = new JTextField();
private JLabel lblSex = new JLabel("性别");
private JRadioButton radMale = new JRadioButton("男");
private JRadioButton radFemale = new JRadioButton("女");
private JLabel lblAge = new JLabel("年龄");
private JTextField txtAge = new JTextField();
private JLabel lblCareer = new JLabel("职业");
private JTextField txtCareer = new JTextField();
private JPanel pnl1 = new JPanel();
private JPanel pnl1_1 = new JPanel(new GridLayout(2, 4));
private JPanel pnlSexGroup = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
public ReaderAddIFrame() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.pnlSexGroup.add(this.radMale);
this.pnlSexGroup.add(this.radFemale);
this.pnl1_1.add(this.lblName);
this.pnl1_1.add(this.txtName);
this.pnl1_1.add(this.lblSex);
this.pnl1_1.add(this.pnlSexGroup);
this.pnl1_1.add(this.lblAge);
this.pnl1_1.add(this.txtAge);
this.pnl1_1.add(this.lblCareer);
this.pnl1_1.add(this.txtCareer);
this.pnl1.add(this.pnl1_1);
this.add(this.pnl1, BorderLayout.CENTER);
this.setSize(600, 400);
this.setVisible(true);
}
public static void main(String[] args) {
new ReaderAddIFrame();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询