求助:java awt的Frame中添加按钮和文本框组件无显示?
用Java编写AWT用户界面处理程序,在一个Frame窗口中放置一个button按钮和一个文本域控件,Frame窗口应当是可以关闭的。当用户单击button按钮时,程序将...
用Java编写AWT用户界面处理程序,在一个Frame窗口中放置一个button按钮和一个文本域控件,Frame窗口应当是可以关闭的。当用户单击button按钮时,程序将按钮上的文字添加到文本域中。
另一种做法 也不行诶http://zhidao.baidu.com/question/278060007.html?fr=im100009
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MyFrame implements ActionListener,WindowListener
{
static String str = "show text!";
static TextField txt = new TextField();
static Button bt = new Button(str);
public static void main(String[] args)
{
Frame f = new Frame("show");
f.setBounds(100, 100, 500, 500);
f.add(bt);
f.add(txt);
bt.addActionListener(new MyFrame());
f.addWindowListener(new MyFrame());
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource()==bt)
{
txt.setText(str);// = str;
}
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
} 展开
另一种做法 也不行诶http://zhidao.baidu.com/question/278060007.html?fr=im100009
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MyFrame implements ActionListener,WindowListener
{
static String str = "show text!";
static TextField txt = new TextField();
static Button bt = new Button(str);
public static void main(String[] args)
{
Frame f = new Frame("show");
f.setBounds(100, 100, 500, 500);
f.add(bt);
f.add(txt);
bt.addActionListener(new MyFrame());
f.addWindowListener(new MyFrame());
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource()==bt)
{
txt.setText(str);// = str;
}
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
} 展开
3个回答
展开全部
你是在那里学的?流程不是很好,不过也是可以实现的,这是我的设计流程:
图形用户界面:
1.声明组件(JButton_按钮,JPanel_容器,JLable_标签,JTextField_文本字段,JTextArea_多行文本框,JPasswordField_密码框,JChecBox_多选按钮,JRadioButton_单选按钮, JComboBox_拆分窗格,JScrollPane_复选框,JList_下拉列表)
2.创建
3.设置布局
4.添加组件
5.面板设置
看我改的:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class MyFrame extends JFrame implements ActionListener,WindowListener
{
static String str = "show text!";
static TextField txt = new TextField(12);
static Button bt = new Button(str);
public MyFrame()
{
setTitle("show");
setBounds(100, 100, 500, 500);
setLayout(new FlowLayout());
add(bt);
add(txt);
bt.addActionListener(this);
addWindowListener(this);
setVisible(true);
}
public static void main(String[] args)
{
new MyFrame();
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource()==bt)
{
txt.setText(str);// = str;
}
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
图形用户界面:
1.声明组件(JButton_按钮,JPanel_容器,JLable_标签,JTextField_文本字段,JTextArea_多行文本框,JPasswordField_密码框,JChecBox_多选按钮,JRadioButton_单选按钮, JComboBox_拆分窗格,JScrollPane_复选框,JList_下拉列表)
2.创建
3.设置布局
4.添加组件
5.面板设置
看我改的:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class MyFrame extends JFrame implements ActionListener,WindowListener
{
static String str = "show text!";
static TextField txt = new TextField(12);
static Button bt = new Button(str);
public MyFrame()
{
setTitle("show");
setBounds(100, 100, 500, 500);
setLayout(new FlowLayout());
add(bt);
add(txt);
bt.addActionListener(this);
addWindowListener(this);
setVisible(true);
}
public static void main(String[] args)
{
new MyFrame();
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource()==bt)
{
txt.setText(str);// = str;
}
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
追问
题目要求awt界面诶,不要用swing的JFrame行不行的呢,
为什么我的代码是界面上没有任何显示的呢
追答
现在java工具包更新了,你那样也是可以完成的。但思路不是很清晰,我总结了一下:
图形用户界面:
1.声明组件(JButton_按钮,JPanel_容器,JLable_标签,JTextField_文本字段,JTextArea_多行文本框,
JPasswordField_密码框,JChecBox_多选按钮,JRadioButton_单选按钮,
JComboBox_拆分窗格,JScrollPane_复选框,JList_下拉列表)
2.创建
3.设置布局
4.添加组件
5.面板设置
绘图原理(1):
Component 类提供了两个和绘图相关最重要的方法:
1.Paint(Graphics g)绘图组件的外观
2.repaint()刷新组件的外观
当组件第一次在屏幕显示的时候,程序会自动的调用Paint()方法来绘制组件
绘图原理(2):
在以下情况Paint()将会调用:
1.窗口最小化,再最大化。
2.窗口的大小发生变化。
3.repaint函数被调用。
Graphics类中的常用方法:
画直线drawLine(int x1,int y1,int x2,int y2)
画矩形边框drawRect(int x,int y,int width,int height)
画椭圆边框drawOval(int x,int y,int width,int height)
画填充矩形fillRect(int x,int y,int width,int height)
画填充椭圆fillOval(int x,int y,int width,int height)
画图片drawImage(Image img,int x,int y)
画字符串drawString(String str,int x,int y)
设置画笔的字体setFont(Font font)
设置画笔的颜色setColor(Color c)
展开全部
窗口关闭好像要加这个差不多的那啥setDefaultCloseOperation (那个啥.EXIT_ON_CLOSE)
而且你这程序结构好乱,MAIN方法里不要这那么多东西,放在最后,直接建立一个类对象方便测试
然后前面把类继承自FRAME 这样就可以直接初始化得到你想要的了
而且你这程序结构好乱,MAIN方法里不要这那么多东西,放在最后,直接建立一个类对象方便测试
然后前面把类继承自FRAME 这样就可以直接初始化得到你想要的了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可能事件编写有问题吧
帮你改了一下
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JPanel;
public class MyFrame implements ActionListener, WindowListener {
static String str = "show text!";
static TextField txt = new TextField();
static Button bt = new Button(str);
public static void main(String[] args) {
Frame f = new Frame("show");
f.setBounds(100, 100, 500, 500);
JPanel jp = new JPanel();
jp.add(bt);
jp.add(txt);
f.add(jp);
// f.add(bt);
// f.add(txt);
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
txt.setText(str);
}
});
f.addWindowListener(new MyFrame());
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
帮你改了一下
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JPanel;
public class MyFrame implements ActionListener, WindowListener {
static String str = "show text!";
static TextField txt = new TextField();
static Button bt = new Button(str);
public static void main(String[] args) {
Frame f = new Frame("show");
f.setBounds(100, 100, 500, 500);
JPanel jp = new JPanel();
jp.add(bt);
jp.add(txt);
f.add(jp);
// f.add(bt);
// f.add(txt);
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
txt.setText(str);
}
});
f.addWindowListener(new MyFrame());
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
追问
啊 一定要用到swing么 一定要弄个panel么
为什么直接不能显示的呢?
追答
你是编applet还是java gui桌面程序?
applet甚至连main都不用。
draw()方法实现init()和paint()就行了。
但java 桌面程序好像一定要JPanel的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询