我正在创建一个程序,使用滚动条选择标签的前景色,三个水平滚动条用于选择颜色的红色绿色和蓝色分量。我需要一个包含滚动条的面板上的标题边框。我只是开始了。我遇到了这个messagePanel的问题。
import java.awt.*;
import javax.swing.*;
public class MessagePanel extends JPanel
{
//The message to be displayed
private String message;
//The x-Coordinate
private int xCoordinate;
//The y-Coordinate
private int yCoordinate;
//Centered
private boolean centered;
//Interval length for moving message horizontally and vertically
private int interval = 10;
public MessagePanel()
{
}
//Construct message panel with a specific message
public MessagePanel(String message)
{
this.message = message;
}
//Return that specific message
public String getMessage()
{
return message;
}
//Set a new message
public void setMessage(String message)
{
this.message = message;
repaint();
}
//Return xCoordinate
public int getXCoordinate()
{
return xCoordinate;
}
//Set a new xCoordinate
public void setXCoordinate(int x)
{
this.xCoordinate = x;
repaint();
}
//Return yCoordinate
public int getYCoordinate()
{
return yCoordinate;
}
//Set a new yCoordinate
public void setYCoordinate(int y)
{
this.yCoordinate = y;
repaint();
}
//Return Centered
public boolean isCentered()
{
return centered;
}
//Set a new Centered
public void setCenetered(boolean centered)
{
this.centered = centered;
}
//Return interval
public int getInterval()
{
return interval;
}
//Set a new interval
public void setInterval(int interval)
{
this.interval = interval;
repaint();
}
//Paint the Message
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (centered)
{
//Current metrics for current font
FontMetrics fm = g.getFontMetrics();
//Find the center location to display
int stringWidth = fm.stringWidth(message);
int stringAscent = fm.getAscent();
//Get the position of the leftmost char in the baseline
int xCoordinate = getWidth() / 2 - stringWidth / 2;
int yCoordinate = getHeight() / 2 + stringAscent / 2;
}
//Draw String
g.drawString(message, xCoordinate, yCoordinate);
}
//Move the message to the left
public void moveLeft()
{
xCoordinate -= interval;
repaint();
}
//Move the message to the right
public void moveRight()
{
xCoordinate += interval;
repaint();
}
//Move the message up
public void moveUp()
{
yCoordinate -= interval;
repaint();
}
//Move the message down
public void moveDown()
{
yCoordinate += interval;
repaint();
}
//Override get method for preferredSize
public Dimension getPreferredSize()
{
return new Dimension(200, 30);
}
}
这可能是ScrollBar类:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class ScrollBar extends JFrame
{
//Creating scroll bars
private JSlider jslHorz1 = new JSlider(JSlider.HORIZONTAL);
private JSlider jslHorz2 = new JSlider(JSlider.HORIZONTAL);
private JSlider jslHorz3 = new JSlider(JSlider.HORIZONTAL);
//Creating the message panel
private MessagePanel messagePanel = new MessagePanel("Show Color");
//Adding scroll bars and message panel
public ScrollBar(){
//Creating UI
setLayout(new BorderLayout(3, 3));
//Adding messagePanel
add(messagePanel, BorderLayout.CENTER);
//Adding the scroll bars
JPanel three = new JPanel(new GridLayout(3,1));
three.add(new JLabel("Red"));
three.add(jslHorz1);
three.add(new JLabel("Green"));
three.add(jslHorz2);
three.add(new JLabel("Blue"));
three.add(jslHorz3);
add(three, BorderLayout.SOUTH);
three.setBorder(new TitledBorder("Choose Color"));
}
//Main method
public static void main(String[] args)
{
ScrollBar frame = new ScrollBar();
frame.setTitle("Exercise 17_15");
frame.setSize(300, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.pack();
frame.setVisible(true);
}
}