📄 9.14-buttondemo.java
字号:
// ButtonDemo.java: Use buttons to move message in a panel
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class ButtonDemo extends JFrame
implements ActionListener
{
// Declare a panel for displaying message
private MessagePanel messagePanel;
// Declare two buttons to move the message left and right
private JButton jbtLeft, jbtRight;
// Main method
public static void main(String[] args)
{
ButtonDemo frame = new ButtonDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ButtonDemo()
{
setTitle("Button Demo");
// Create a MessagePanel instance and set colors
messagePanel = new MessagePanel("Welcome to Java");
messagePanel.setBackground(Color.yellow);
// Create Panel jpButtons to hold two Buttons "<=" and "right =>"
JPanel jpButtons = new JPanel();
jpButtons.setLayout(new FlowLayout());
jpButtons.add(jbtLeft = new JButton());
jpButtons.add(jbtRight = new JButton());
// Set button text
jbtLeft.setText("<=");
jbtRight.setText("=>");
// Set keyboard mnemonics
jbtLeft.setMnemonic('L');
jbtRight.setMnemonic('R');
// Set icons
//jbtLeft.setIcon(new ImageIcon("images/left.gif"));
//jbtRight.setIcon(new ImageIcon("images/right.gif"));
// Set toolTipText on the "<=" and "=>" buttons
jbtLeft.setToolTipText("Move message to left");
jbtRight.setToolTipText("Move message to right");
// Place panels in the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(messagePanel, BorderLayout.CENTER);
getContentPane().add(jpButtons, BorderLayout.SOUTH);
// Register listeners with the buttons
jbtLeft.addActionListener(this);
jbtRight.addActionListener(this);
}
// Handle button events
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtLeft)
{
left();
}
else if (e.getSource() == jbtRight)
{
right();
}
}
// Move the message in the panel left
private void left()
{
int x = messagePanel.getXCoordinate();
if (x > 10)
{
// Shift the message to the left
messagePanel.setXCoordinate(x-10);
messagePanel.repaint();
}
}
// Move the message in the panel right
private void right()
{
int x = messagePanel.getXCoordinate();
if (x < getSize().width - 20)
{
// Shift the message to the right
messagePanel.setXCoordinate(x+10);
messagePanel.repaint();
}
}
}
// MessagePanel: Display a message on a JPanel
class MessagePanel extends JPanel
{
private String message = "Welcome to Java"; // Message to display
// (x, y) coordinates where the message is displayed
private int xCoordinate = 20;
private int yCoordinate = 20;
// Indicating whether the message is displayed in the center
private boolean centered;
// Default constructor
public MessagePanel()
{
}
// Contructor with a message parameter
public MessagePanel(String message)
{
this.message = message;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public int getXCoordinate()
{
return xCoordinate;
}
public void setXCoordinate(int x)
{
this.xCoordinate = x;
}
public int getYCoordinate()
{
return yCoordinate;
}
public void setYCoordinate(int y)
{
this.yCoordinate = y;
}
public boolean isCentered()
{
return centered;
}
public void setCentered(boolean centered)
{
this.centered = centered;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (centered)
{
// Get font metrics for the current font
FontMetrics fm = g.getFontMetrics();
// Find the center location to display
int w = fm.stringWidth(message); // Get the string width
int h = fm.getAscent(); // Get the string height
xCoordinate = (getWidth()-w)/2;
yCoordinate = (getHeight()+h)/2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
public Dimension getPreferredSize()
{
return new Dimension(200, 100);
}
public Dimension getMinimumSize()
{
return new Dimension(200, 100);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -