| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2262 人关注过本帖
标题:[求助]怎样设置面板大小?
只看楼主 加入收藏
水影月圆
Rank: 4
等 级:贵宾
威 望:11
帖 子:738
专家分:0
注 册:2005-8-2
收藏
得分:0 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonDemo extends JPanel implements ActionListener
{
JButton b1,b2;
String message="";
int row,col;
public ButtonDemo()
{
setLayout(null);
b1=new JButton("First Button");
b1.setMnemonic(KeyEvent.VK_CONTROL|KeyEvent.VK_F);
b1.setActionCommand("first");
b2=new JButton("Second Button");
b2.setMnemonic(KeyEvent.VK_S);
b2.setActionCommand("second");
b1.addActionListener(this);
b2.addActionListener(this);

b1.setBounds(100,20,100,100);
b2.setBounds(300,20,100,100);
add(b1);
add(b2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("first"))
{
message=new String("第一个按钮被按下!");
row=150;
col=150;
}
else if(e.getActionCommand().equals("second"))
{
message=new String("第二个按钮被按下!");
row=150;
col=150;
}
repaint();
}
public static void main(String args[])
{
JFrame jf=new JFrame("按钮测试");
jf.getContentPane().add(new ButtonDemo());
jf.setSize(500,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(message,col,row);
repaint();
}
}
这是我以前学习时打的代码 你看看 我布局用的null 大小用setBounds控制的


子非鱼,安知鱼之江湖?子非我,安知我之功夫 http://20681.
2006-06-17 13:23
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
绝是行的,怎么可能不行,我写个试例给你看看
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestLayout extends JFrame implements ActionListener {
private JButton test,change;
private JTextField x,y,width,height;
private JPanel main;
public TestLayout(){
initWindow();
}
private void initWindow(){
main=new JPanel(null);
test=new JButton("测试");
change=new JButton("更新");
change.addActionListener(this);
test.setBounds(100,100,100,100);
main.add(test);
x=new JTextField("100");
y=new JTextField("100");
width=new JTextField("100");
height=new JTextField("100");
JPanel bottom=new JPanel();
bottom.add(new JLabel("x="));
bottom.add(x);
bottom.add(new JLabel("y="));
bottom.add(y);
bottom.add(new JLabel("width="));
bottom.add(width);
bottom.add(new JLabel("height="));
bottom.add(height);
bottom.add(change);
Container c=this.getContentPane();
c.add(main,BorderLayout.CENTER);
c.add(bottom,BorderLayout.SOUTH);
this.setBounds(100,100,400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==change){
int x1=Integer.parseInt(x.getText().trim());
int y1=Integer.parseInt(y.getText().trim());
int width1=Integer.parseInt(width.getText().trim());
int height1=Integer.parseInt(height.getText().trim());
test.setBounds(x1,y1,width1,height1);
main.removeAll();
main.add(test);
SwingUtilities.updateComponentTreeUI(this);
}
}
public static void main(String[] args){
new TestLayout();
}
}

可惜不是你,陪我到最后
2006-06-17 13:30
快速回复:[求助]怎样设置面板大小?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014438 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved