当前位置: 技术问答>java相关
求救,关于gui
来源: 互联网 发布时间:2015-06-01
本文导语: 如下程序,为什么无法显示jlabel 和jbutton, 请指教 package gy; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** 超类:JDialog 类名:MessageDialog showMessage()方法显示消息对话框 */ public class Messag...
如下程序,为什么无法显示jlabel 和jbutton,
请指教
package gy;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
超类:JDialog
类名:MessageDialog
showMessage()方法显示消息对话框
*/
public class MessageDialog extends JDialog
{
/**
*type: = 1 - 确定对话框
* = 2 - 确定、取消对话框
* = 3 - input对话框
*/
//private JFrame f1;
Container con;
static final String caption = new String("在此输入:");
JLabel lb1 = new JLabel(caption);
//JLabel lbMessage = null;//要显示的信息
JTextField tEnter = new JTextField();//输入文本框
JButton bOK = new JButton("确定");//确认按钮
JButton bCancel = new JButton("取消");//取消按钮
String retValue;//返回的值
JPanel pan = new JPanel();
MessageDialog(JFrame f,String title)
{
super(f,title);
}
public int showMessage(String message,int type)
{
con = getContentPane();
pan.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH; // components grow in both dimensions
c.insets = new Insets(5,5,5,5); // 5-pixel margins on all sides
switch(type){
default:
System.out.println("111111111111");
c.gridx = 0; c.gridy = 0; c.gridwidth = 5; c.gridheight=4;
c.weightx = c.weighty = 1.0;
pan.add(new JLabel(message), c);
c.gridx = 3; c.gridy = 7;c.gridwidth = 2; c.gridheight = 1;
pan.add(bOK,c);
break;
}
con.add(pan);
System.out.println("111111111111");
pack();
return 1;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
MessageDialog n = new MessageDialog(f,"test");
System.out.println("asdasdasd");
n.showMessage("this is a test message",1);
f.setSize(200,100);
f.show();
}
}
请指教
package gy;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
超类:JDialog
类名:MessageDialog
showMessage()方法显示消息对话框
*/
public class MessageDialog extends JDialog
{
/**
*type: = 1 - 确定对话框
* = 2 - 确定、取消对话框
* = 3 - input对话框
*/
//private JFrame f1;
Container con;
static final String caption = new String("在此输入:");
JLabel lb1 = new JLabel(caption);
//JLabel lbMessage = null;//要显示的信息
JTextField tEnter = new JTextField();//输入文本框
JButton bOK = new JButton("确定");//确认按钮
JButton bCancel = new JButton("取消");//取消按钮
String retValue;//返回的值
JPanel pan = new JPanel();
MessageDialog(JFrame f,String title)
{
super(f,title);
}
public int showMessage(String message,int type)
{
con = getContentPane();
pan.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH; // components grow in both dimensions
c.insets = new Insets(5,5,5,5); // 5-pixel margins on all sides
switch(type){
default:
System.out.println("111111111111");
c.gridx = 0; c.gridy = 0; c.gridwidth = 5; c.gridheight=4;
c.weightx = c.weighty = 1.0;
pan.add(new JLabel(message), c);
c.gridx = 3; c.gridy = 7;c.gridwidth = 2; c.gridheight = 1;
pan.add(bOK,c);
break;
}
con.add(pan);
System.out.println("111111111111");
pack();
return 1;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
MessageDialog n = new MessageDialog(f,"test");
System.out.println("asdasdasd");
n.showMessage("this is a test message",1);
f.setSize(200,100);
f.show();
}
}
|
你只show了f ,并没有showDialog;
不防在main()中最后加上
n.show();
enjoy
不防在main()中最后加上
n.show();
enjoy
|
public void init(){
setLayout(new FlowLayout());
getContentPane().add(lb1);
getContentPane().add(tEnter);
getContentPane().add(bOK);
getContentPane().add(bCancel);
}
MessageDialog(JFrame f,String title)
{
super(f,title);
init();
}
setLayout(new FlowLayout());
getContentPane().add(lb1);
getContentPane().add(tEnter);
getContentPane().add(bOK);
getContentPane().add(bCancel);
}
MessageDialog(JFrame f,String title)
{
super(f,title);
init();
}
|
好象应该有比如这样的东西把你的东西加进去
Pan.add(lb1, null);
..........
Pan.add(lb1, null);
..........
|
在java中,你想把控件加入到JPanel或者JFrame中,只实例化控件是不行的,要显视的加入才行。如果dialog窗口出现则应该如hegum所说
pan.add(lba,null);
pan.add(lba,null);