import?java.awt.*;
創(chuàng)新互聯(lián)公司從2013年成立,先為利津等服務(wù)建站,利津等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為利津企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
import?java.awt.event.*;
import?javax.swing.*;
class?aa?implements?ActionListener?{
JButton?jb;
public?aa(JButton?jb)?{//構(gòu)造器,把JButton傳遞進(jìn)來(lái),
this.jb?=?jb;
}
public?void?actionPerformed(ActionEvent?e)?{
jb.setVisible(false);//設(shè)置jbutton不可見(jiàn)
}
}
public?class?J1?extends?JFrame{
JButton?j1?=?new?JButton("Game1");
JButton?j2?=?new?JButton("Game2");
J1(){
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container?c?=?getContentPane();
c.setLayout(new?FlowLayout());
c.add(j1);
c.add(j2);
j1.addActionListener(new?aa(j1));//把j1這個(gè)按鈕傳遞進(jìn)過(guò)去
j2.addActionListener(new?aa(j2));//把j2這個(gè)按鈕傳遞進(jìn)過(guò)去
setSize(400,400);
setVisible(true);
}
public?static?void?main(String[]?args)?{
new?J1();
}
}
效果圖
把frame=new subJFrame("DrawShapes");改成frame=new JFrame("DrawShapes");
程序基本沒(méi)問(wèn)題,在public void paint(Graphics g)中加上如下程序就可以了。
public void paint(Graphics g){
switch(i){
case 1: g.drawOval(20,20,40,40);break;
case 2: g.drawRect(20,20,40,40);break;
case 3: g.drawOval(20,30,40,50);break;
case 4: g.drawLine(20,20,40,40);break;
}
}
你好,在構(gòu)造方法FrameOut()中調(diào)用setLayout()方法加入一種控件布局形式,例如加入setLayout(new FlowLayout());即可以流布局的形式顯示控件。
完整代碼如下:
import java.awt.*;
import java.awt.event.*;
public class ApplicationInOut{
public static void main(String args[ ]){
new FrameInOut();
}
}
class FrameInOut extends Frame implements ActionListener{
Label prompt;
TextField input,output;
FrameInOut( ){
super("圖形界面的Java Application程序");
prompt=new Label("Java 是面向?qū)ο蟮恼Z(yǔ)言嗎?");
input=new TextField(6);
output=new TextField(20);
add(prompt);
add(input);
add(output);
input.addActionListener(this);
setLayout(new FlowLayout()); //此處即為添加布局形式
setSize(800,600);
setVisible(true); //show( );
}
public void actionPerformed(ActionEvent e){
output.setText(input.getText()+"OK!");
}
}
import?java.awt.Container;
import?java.awt.FlowLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.SwingUtilities;
class?aa?implements?ActionListener
{
JButton?a,?b;
public?aa(JButton?c,?JButton?d)
{
a?=?c;
b?=?d;
}
public?void?actionPerformed(ActionEvent?e)
{
a.setVisible(false);
b.setVisible(false);
}
}
public?class?J1?extends?JFrame
{
private?static?final?long?serialVersionUID?=?1L;
JButton?j1?=?new?JButton("Game1");
JButton?j2?=?new?JButton("Game2");
JButton?j3?=?new?JButton("點(diǎn)擊搖骰子");
J1()
{
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,?400);
setVisible(true);
final?Container?c?=?getContentPane();
c.setLayout(new?FlowLayout());
c.add(j1);
c.add(j2);
j1.addActionListener(new?ActionListener()
{
public?void?actionPerformed(ActionEvent?arg0)
{
c.add(new?JLabel("Game1"));
j3.setVisible(true);
c.add(j3);
}
});
j1.addActionListener(new?aa(j1,?j2));
j2.addActionListener(new?aa(j1,?j2));
j3.addActionListener(new?ActionListener()
{
public?void?actionPerformed(ActionEvent?e)
{
int?a?=?(int)?(Math.random()?*?6);
switch(a)
{
case?0:
c.add(new?JLabel("1"));
break;
case?1:
c.add(new?JLabel("2"));
break;
case?2:
c.add(new?JLabel("3"));
break;
case?3:
c.add(new?JLabel("4"));
break;
case?4:
c.add(new?JLabel("5"));
break;
case?5:
c.add(new?JLabel("6"));
break;
}
SwingUtilities.updateComponentTreeUI(c);
}
});
}
public?static?void?main(String[]?args)
{
new?J1();
}
}
代碼如下:
import?java.io.*;
import?java.awt.*;
import?java.awt.event.*;
public?class?jtxtfm{
public?static?void?main(String?args[]){
jtxtfrm?fm=new?jtxtfrm();
}
}
class?jtxtfrm?extends?Frame?implements?ActionListener{
FileDialog?op,sv;
Button?btn1,btn2,btn3;
TextArea?tarea;
jtxtfrm(){
super("讀寫(xiě)文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600,300);
setVisible(true);
btn1=new?Button("打開(kāi)");
btn2=new?Button("保存");
btn3=new?Button("關(guān)閉");
tarea=new?TextArea("");
add(btn1);add(btn2);add(btn3);add(tarea);
tarea.setBounds(30,50,460,220);
btn1.setBounds(520,60,50,30);
btn2.setBounds(520,120,50,30);
btn3.setBounds(520,180,50,30);
op=new?FileDialog(this,"打開(kāi)",FileDialog.LOAD);
sv=new?FileDialog(this,"保存",FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(
new?WindowAdapter(){
public?void?windowClosing(WindowEvent?e){
setVisible(false);
System.exit(0);
}
}????
);
}
public?void?actionPerformed(ActionEvent?e){
if(e.getSource()==btn1){
String?str;
op.setVisible(true);
try{
File?f1=new?File(op.getDirectory(),op.getFile());
FileReader?fr=new?FileReader(f1);
BufferedReader?br=new?BufferedReader(fr);
tarea.setText("");
while((str=br.readLine())!=null)tarea.append(str+'\n');
fr.close();
}
catch(Exception?e1)
{}
}
if(e.getSource()==btn2){
sv.setVisible(true);
try{
File?f1=new?File(sv.getDirectory(),sv.getFile());
FileWriter?fw=new?FileWriter(f1);
BufferedWriter?bw=new?BufferedWriter(fw);
String?gt=tarea.getText();
bw.write(gt,0,gt.length());
bw.flush();
fw.close();
}
catch?(?Exception?e2)
{}
}
if(e.getSource()==btn3){
System.exit(0);
}
}
}
效果圖: