这样:
import javaawt;
import javaawtevent;
import javaxswing;
import javaawtGraphics;
public class MainClass extends JFrame {
public JComboBox box;
int flag = 0;
jpNewPanel jpNewPanel;
public static void main(String[] args) {
MainClass frame = new MainClass();
framesetBounds(650,300,550,550);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetTitle("信号灯");
framesetVisible(true);
}
public MainClass() {
box = new JComboBox();
boxaddItem("请选择");
boxaddItem("红灯");
boxaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = boxgetSelectedIndex();
jpNewPanelrepaint();
}
});
boxaddItem("黄灯");
boxaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = boxgetSelectedIndex();
jpNewPanelrepaint();
}
});
boxaddItem("绿灯");
boxaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = boxgetSelectedIndex();
jpNewPanelrepaint();
}
});
add(box, BorderLayoutNORTH);
jpNewPanel = new jpNewPanel();
add(jpNewPanel, BorderLayoutCENTER);
}
class jpNewPanel extends JPanel {
protected void paintComponent(Graphics g) {
superpaintComponent(g);
gdrawOval(150, 0, 120, 120);
if (flag == 1) {
gsetColor(ColorRED);
gfillOval(150, 0, 120, 120);
} else if (flag == 2) {
gsetColor(ColorYELLOW);
gfillOval(150, 0, 120, 120);
} else if (flag == 3) {
gsetColor(ColorGREEN);
gfillOval(150, 0, 120, 120);
}
}
}
}
扩展资料:
注意事项
每个Road对象都有一个name成员变量来代表方向,有一个vehicles成员变量来代表方向上的车辆集合。
在Road对象的构造方法中启动一个线程每隔一个随机的时间向vehicles集合中增加一辆车(用一个“路线名_id”形式的字符串进行表示)。
在Road对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。
package interframe;import javaawtBorderLayout;
import javaawteventActionEvent;
import javaawteventActionListener;import javaxswingJFrame;
import javaxswingJMenu;
import javaxswingJMenuBar;
import javaxswingJMenuItem;
import javaxswingJOptionPane;public class MenuTest { /
@param args
/
JFrame frame;
JMenuBar mb;
JMenu m;
JMenuItem mi1;
JMenuItem mi2; public MenuTest() {
initFrame();
initAction();
} public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("学生查询");
mi1 = new JMenuItem("确认");
mi2 = new JMenuItem("取消"); madd(mi1);
madd(mi2);
mbadd(m);
frameadd(mb, BorderLayoutNORTH);
framesetSize(300, 300);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
} public void initAction() {
mi1addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Systemoutprintln("click");
JOptionPaneshowMessageDialog(null, "你点击了确定按钮");
}
});
mi2addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPaneshowMessageDialog(null, "你点击了取消按钮");
}
});
} public static void main(String[] args) {
// TODO Auto-generated method stub
new MenuTest();
}}
import javaxswing;
import javaawt;
import javaawtevent;
public class Test3 extends JFrame implements ActionListener{
JMenu m;
JMenuItem mi1,mi2;
JMenuBar mb;
public Test3(){
m = new JMenu("学生查询");
mi1 = new JMenuItem("确认");
mi2 = new JMenuItem("取消");
mb = new JMenuBar();
madd(mi1);
madd(mi2);
mbadd(m);
thissetJMenuBar(mb);
thissetSize(400,300);
thissetLocationRelativeTo(null);//窗口居中
thissetVisible(true);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
mi1addActionListener(this);
mi2addActionListener(this);
}
public static void main(String[] args){
new Test3();
}
public void actionPerformed(ActionEvent ae){
if(aegetSource()==mi1){
JOptionPaneshowMessageDialog(null, "你点击了确定按钮"); }else{
JOptionPaneshowMessageDialog(null, "你点击了取消按钮"); }
}
}
参考代码和详细的注释如下
//导入开发swing图形界面常用的3个包import javaawt;
import javaawtevent;
import javaxswing;
//DemoFrame继承自JFrame
public class DemoFrame extends JFrame {
public DemoFrame() {
JButton jbHello = new JButton("Say Hello");//创建按钮, 按钮上的文字是 Say Hello
//当按钮点击时,d出对话框
jbHelloaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//对话框显示
JOptionPaneshowMessageDialog(null, "Hello Java GUI", "Hi", JOptionPaneINFORMATION_MESSAGE);
}
});
getContentPane()add(jbHello);// 获取内容面板, 并把按钮加入到窗口的内容面板
setLayout(new FlowLayout());//设置窗口的布局为流式布局,从左到右排列
setTitle("Title");//设置窗口的标题
setSize(260, 180);//设置窗口的宽高
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);//点击窗口的关闭按钮时,结束
}
public static void main(String[] args) {
EventQueueinvokeLater(new Runnable() {
public void run() {
new DemoFrame()setVisible(true);// 创建窗口并设置可见
}
});
}
}
VC2010
生成的工程默认是
Unicode
编码,字符串必须是
Unicode
字符,在前面加上
L:
MessageBox(NULL,
L"Hello
World!",
L"我的第一个窗口程序",
MB_OK);
一般来说,用
VC
开发程序都不这样写,而是应该是使用微软定义的数据类型和宏,这样便能同时编译
ANSI
版本,不用修改源文件。须包含头文件
tcharh,对于字符串应使用
_T
或
TEXT
宏将其包含,如
_T("Hello")。当定义
Unicode
时,等同于
L"Hello";未定义时等同于
"Hello"。所以一般都写成这样:
#include
#include
void
main()
{
MessageBox(NULL,
_T("Hello
World!"),
_T("我的第一个窗口程序"),
MB_OK);
}
当然,上面说过,像楼上那样使用
TEXT
宏也是可以的,两者是同一个东西。
package window;
import javaawtContainer;
import javaawtFlowLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJTextArea;
import javaxswingJTextField;
public class Window extends JFrame{
public static int num = 0;
public static void main(String[] args) {
new Window();
}
public Window(){
//设置x y坐标
thissetLocation(400, 500);
//设置窗口大小
thissetSize(512, 512);
//设置窗口可见
thissetVisible(true);
//设置窗口关闭后程序停止
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
//设置窗口不可移动
thissetResizable(false);
JButton but1 = new JButton("按钮");
final JTextField text1 = new JTextField(10);
以上就是关于JAVA 编写一个带有窗口的应用程序全部的内容,包括:JAVA 编写一个带有窗口的应用程序、Java编写一个简单的窗口程序、Java编写一个简单的窗口程序,流程是什么等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)