编写Java程序,在窗体的中央显示一行文字(文字内容自定),并且文字会在窗体内左右漂移。

发布时间:2024-05-13 22:23 发布:上海旅游网

问题描述:

问题解答:

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();
}
}

热点新闻