按钮 题目:设计一个界面,第一行含有三个按钮,第二行正中间含有一个按钮,第三行含有两个按钮。这道题目不使用面板能不能做出来?怎么换行? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以啊,运用网格/网格包布局管理器就行,不过运用网格包管理器更强大,这是我用网格包写的:import java.awt.Button;import java.awt.Component;import java.awt.Frame;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Tt extends Frame { static Button b1,b2,b3,b4,b5,b6; static GridBagLayout gb=new GridBagLayout(); GridBagConstraints gbc=new GridBagConstraints(); public static void main(String[]args){ Tt t=new Tt(); t.setSize(500, 500); t.setLayout(gb); t.setVisible(true); t.run(); } public Tt(){ b1=new Button("按钮一"); b2=new Button("按钮二"); b3=new Button("按钮三"); b4=new Button("按钮四"); b5=new Button("按钮五"); b6=new Button("按钮六"); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){ System.exit(0); } }); } public void run(){ addComponent(b1,0,0,2,1); addComponent(b2,0,2,2,1); addComponent(b3,0,4,2,1); addComponent(b4,1,2,2,1); addComponent(b5,2,0,2,1); addComponent(b6,2,2,2,1); } public void addComponent(Component c,int row,int col,int nrow,int ncol){ gbc.gridx=col; gbc.gridy=row; gbc.gridheight=ncol; gbc.gridwidth=nrow; gb.setConstraints(c, gbc); add(c); }} import java.awt.Button;import java.awt.Component;import java.awt.Frame;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Tt extends Frame { static Button b1,b2,b3,b4,b5,b6; static GridBagLayout gb=new GridBagLayout(); GridBagConstraints gbc=new GridBagConstraints(); public static void main(String[]args){ Tt t=new Tt(); t.setSize(500, 500); t.setLayout(gb); t.setVisible(true); t.run(); } public Tt(){ b1=new Button("按钮一"); b2=new Button("按钮二"); b3=new Button("按钮三"); b4=new Button("按钮四"); b5=new Button("按钮五"); b6=new Button("按钮六"); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){ System.exit(0); } }); } public void run(){ addComponent(b1,0,0,2,1); addComponent(b2,0,2,2,1); addComponent(b3,0,4,2,1); addComponent(b4,1,2,2,1); addComponent(b5,2,0,2,1); addComponent(b6,2,2,2,1); } public void addComponent(Component c,int row,int col,int nrow,int ncol){ gbc.gridx=col; gbc.gridy=row; gbc.gridheight=ncol; gbc.gridwidth=nrow; gb.setConstraints(c, gbc); add(c); }} setLayout(null)然后自己布局,so easy的问题 //你最后两个是分两边吗?你看看吧。想怎么放都行public class JButtonTest extends JFrame { JButton bt1=new JButton("一一"); JButton bt2=new JButton("一二"); JButton bt3=new JButton("一三"); JButton bt21=new JButton("二一"); JButton bt31=new JButton("三一"); JButton bt32=new JButton("三二"); public JButtonTest(){ this.setSize(300, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); init(); } public void init(){ JPanel panel=new JPanel(); panel.setLayout(new GridBagLayout()); panel.add(bt1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0)); panel.add(bt2, new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0)); panel.add(bt3, new GridBagConstraints(2, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0)); panel.add(bt21, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0)); panel.add(bt31, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0)); panel.add(new JPanel(), new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0)); panel.add(bt32, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0)); this.add(panel); } public static void main(String [] args){ JButtonTest bt=new JButtonTest(); }} java文件操作 求助 java 每次访问sql sever 2000数据库后 需要结束进程么? jsp用户登录怎么验证?请教 很急的一个java小问题!!!请大家帮一下忙! 彻底晕了,大家帮帮看看。。。。 如何连接两个语言之间的通信 三个问题 100分求文件目录搜索原代码 Object 类的 clone 方法如何使用 你想成为一流的Java开发程序员吗? 一个初始化的问题… java反射返回问题 3
import java.awt.Button;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Tt extends Frame {
static Button b1,b2,b3,b4,b5,b6;
static GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
public static void main(String[]args){
Tt t=new Tt();
t.setSize(500, 500);
t.setLayout(gb);
t.setVisible(true);
t.run();
}
public Tt(){
b1=new Button("按钮一");
b2=new Button("按钮二");
b3=new Button("按钮三");
b4=new Button("按钮四");
b5=new Button("按钮五");
b6=new Button("按钮六");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
public void run(){
addComponent(b1,0,0,2,1);
addComponent(b2,0,2,2,1);
addComponent(b3,0,4,2,1);
addComponent(b4,1,2,2,1);
addComponent(b5,2,0,2,1);
addComponent(b6,2,2,2,1);
}
public void addComponent(Component c,int row,int col,int nrow,int ncol){
gbc.gridx=col;
gbc.gridy=row;
gbc.gridheight=ncol;
gbc.gridwidth=nrow;
gb.setConstraints(c, gbc);
add(c);
}}
import java.awt.Button;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Tt extends Frame {
static Button b1,b2,b3,b4,b5,b6;
static GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
public static void main(String[]args){
Tt t=new Tt();
t.setSize(500, 500);
t.setLayout(gb);
t.setVisible(true);
t.run();
}
public Tt(){
b1=new Button("按钮一");
b2=new Button("按钮二");
b3=new Button("按钮三");
b4=new Button("按钮四");
b5=new Button("按钮五");
b6=new Button("按钮六");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
public void run(){
addComponent(b1,0,0,2,1);
addComponent(b2,0,2,2,1);
addComponent(b3,0,4,2,1);
addComponent(b4,1,2,2,1);
addComponent(b5,2,0,2,1);
addComponent(b6,2,2,2,1);
}
public void addComponent(Component c,int row,int col,int nrow,int ncol){
gbc.gridx=col;
gbc.gridy=row;
gbc.gridheight=ncol;
gbc.gridwidth=nrow;
gb.setConstraints(c, gbc);
add(c);
}}
然后自己布局,so easy的问题
public class JButtonTest extends JFrame {
JButton bt1=new JButton("一一");
JButton bt2=new JButton("一二");
JButton bt3=new JButton("一三");
JButton bt21=new JButton("二一");
JButton bt31=new JButton("三一");
JButton bt32=new JButton("三二");
public JButtonTest(){
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
init();
}
public void init(){
JPanel panel=new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(bt1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
panel.add(bt2, new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
panel.add(bt3, new GridBagConstraints(2, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
panel.add(bt21, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
panel.add(bt31, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
panel.add(new JPanel(), new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
panel.add(bt32, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
this.add(panel);
}
public static void main(String [] args){
JButtonTest bt=new JButtonTest();
}
}