扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
import java.awt.*;
创新互联公司从2013年成立,先为利津等服务建站,利津等地企业,进行企业商务咨询服务。为利津企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
import java.awt.event.*;
import javax.swing.*;
class aa implements ActionListener {
JButton jb;
public aa(JButton jb) {//构造器,把JButton传递进来,
this.jb = jb;
}
public void actionPerformed(ActionEvent e) {
jb.setVisible(false);//设置jbutton不可见
}
}
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这个按钮传递进过去
j2.addActionListener(new aa(j2));//把j2这个按钮传递进过去
setSize(400,400);
setVisible(true);
}
public static void main(String[] args) {
new J1();
}
}
效果图
把frame=new subJFrame("DrawShapes");改成frame=new JFrame("DrawShapes");
程序基本没问题,在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;
}
}
你好,在构造方法FrameOut()中调用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 是面向对象的语言吗?");
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("点击摇骰子");
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("读写文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600,300);
setVisible(true);
btn1=new Button("打开");
btn2=new Button("保存");
btn3=new Button("关闭");
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,"打开",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);
}
}
}
效果图:
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流