编写Java程序,在窗体的中央显示一行文字(文字内容自定),并且文字会在窗体内左右漂移。
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Piaoyi extends JFrame implements ActionListener
{
JPanel jp=new JPanel();
JButton b1=new JButton("左");
JButton b2=new JButton("中");
JButton b3=new JButton("右");
JLabel l=new JLabel("我只会在这里面漂移...");
public AAA()
{
jp.setLayout(new FlowLayout());
jp.add(b1);
jp.add(b2);
jp.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
this.add(jp,BorderLayout.NORTH);
this.add(l,BorderLayout.CENTER);
this.setBounds(100,100,260,160);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
l.setHorizontalAlignment(JLabel.LEFT);
}
if(e.getSource()==b2)
{
l.setHorizontalAlignment(JLabel.CENTER);
}
if(e.getSource()==b3)
{
l.setHorizontalAlignment(JLabel.RIGHT);
}
}
public static void main(String[] args)
{
new Piaoyi();
}
}
import java.awt.*;
import java.awt.event.*;
public class Piaoyi extends JFrame implements ActionListener
{
JPanel jp=new JPanel();
JButton b1=new JButton("左");
JButton b2=new JButton("中");
JButton b3=new JButton("右");
JLabel l=new JLabel("我只会在这里面漂移...");
public AAA()
{
jp.setLayout(new FlowLayout());
jp.add(b1);
jp.add(b2);
jp.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
this.add(jp,BorderLayout.NORTH);
this.add(l,BorderLayout.CENTER);
this.setBounds(100,100,260,160);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
l.setHorizontalAlignment(JLabel.LEFT);
}
if(e.getSource()==b2)
{
l.setHorizontalAlignment(JLabel.CENTER);
}
if(e.getSource()==b3)
{
l.setHorizontalAlignment(JLabel.RIGHT);
}
}
public static void main(String[] args)
{
new Piaoyi();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |