Java 我为什么会受伤以及如何修复

Java 我为什么会受伤以及如何修复,java,compiler-errors,Java,Compiler Errors,这是我的错误 Note: Question2. java uses unchecked or unsafe operations. Note: Recompile with -Xlint : unchecked for details. 这是我的代码: import javax.swing.*; import javax.swing.border.TitledBorder; import java.awt.*; import java.awt.event.ActionListener; imp

这是我的错误

Note: Question2. java uses unchecked or unsafe operations.
Note: Recompile with -Xlint : unchecked for details.
这是我的代码:

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Question2 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;

// Declare Combo Box,radioButtons,CheckBox, TextField, Interval
private JTextField jtfNewMessage = new JTextField(8);
private JComboBox jcboInterval = new JComboBox();
private JRadioButton jrbRed = new JRadioButton("Red");
private JRadioButton jrbGreen = new JRadioButton("Green");
private JRadioButton jrbBlue = new JRadioButton("Blue");
private JCheckBox jchkCentered = new JCheckBox("Center");
private JCheckBox jchkBold = new JCheckBox("Bold");
private JCheckBox jchkItalic = new JCheckBox("Italic");

// Declare the Font name, Font style, Font size 
private String fontName = "SansSerif";
private int fontStyle = Font.PLAIN;
private int fontSize = 12;
 
/** Default constructor */
public Question2() {
setTitle("Question2");

// Create a MessagePanel instance and set colors
messagePanel = new MessagePanel("Welcome to Java");
messagePanel.setBackground(Color.white);

// 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("image/left.gif"));
//jbtRight.setIcon(new ImageIcon("image/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);

/** 1.Add a text field labeled “New Message.\
 *    Upon typing a new message in the text field and pressing the Enter
 *    key, the new message is displayed in the message panel.
 */
jpButtons.add(new JLabel("Enter a new message"));
jpButtons.add(jtfNewMessage);

jtfNewMessage.addActionListener(this);

/** 2.Add a combo box label “Interval\uFFFD that enables the user to select
 * new interval for moving the message. The selection values range from
 * 10 to 100 with interval 5. The user can also type a new
 *  interval in the combo box.
 */
 jpButtons.add(new JLabel("Select an interval"));
 jpButtons.add(jcboInterval);
 for (int interval = 5; interval <= 100; interval += 5)
  jcboInterval.addItem(interval + "");
 
 jcboInterval.addActionListener(this);
 
 /**
 * 3.Add three radio buttons that enable the user to select the foreground
 * color for the message as Red, Green, and Blue.
 */
 JPanel panel = new JPanel();
 getContentPane().add(panel, BorderLayout.NORTH);
 
 panel.add(jrbRed);
 panel.add(jrbGreen);
 panel.add(jrbBlue);
 ButtonGroup btg = new ButtonGroup();
 btg.add(jrbRed);
 btg.add(jrbGreen);
 btg.add(jrbBlue);
 jrbRed.addActionListener(this);
 jrbGreen.addActionListener(this);
 jrbBlue.addActionListener(this);
 
 /**
 * 4.Add three check boxes that enable the user to center the message
 * and display it in italic or bold.
 */
 panel.add(jchkCentered);
 panel.add(jchkBold);
 panel.add(jchkItalic);
 jchkCentered.addActionListener(this);
 jchkBold.addActionListener(this);
 jchkItalic.addActionListener(this);
 
 /**
 * 5.Add a border titled Message Panel on the message panel.
 */
 messagePanel.setBorder(new TitledBorder("Message Panel"));
 jpButtons.setBorder(new TitledBorder("South Panel"));
 panel.setBorder(new TitledBorder("North Panel"));
 
 this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
 }
 
 /** Main method */
 public static void main(String[] args) {
 Question2 frame = new Question2();
 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 frame.setSize(520, 200);
 frame.setVisible(true);
 }
 
  /** Handle button events */
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbtLeft) {
      messagePanel.moveLeft();
      messagePanel.repaint();
    }
    else if (e.getSource() == jbtRight) {
      messagePanel.moveRight();
      messagePanel.repaint();
    }
    else if (e.getSource() == jtfNewMessage) {
      messagePanel.setMessage(jtfNewMessage.getText());
      messagePanel.repaint();
    }
    else if (e.getSource() == jcboInterval) {
      messagePanel.setInterval(
        Integer.parseInt((String)(jcboInterval.getSelectedItem())));
      messagePanel.repaint();
    }
    else if (e.getSource() == jrbRed) {
      messagePanel.setForeground(Color.red);
    }
    else if (e.getSource() == jrbGreen) {
      messagePanel.setForeground(Color.green);
    }
    else if (e.getSource() == jrbBlue) {
      messagePanel.setForeground(Color.blue);
    }
    else if (e.getSource() == jchkCentered) {
      if (jchkCentered.isSelected())
        messagePanel.setCentered(true);
      else
        messagePanel.setCentered(false);
   
      messagePanel.repaint();
    }
    else if ((e.getSource() == jchkBold) ||
             (e.getSource() == jchkItalic)) {
      
        fontStyle = Font.PLAIN;
    
      // Determine a font style
      if (jchkBold.isSelected())
        fontStyle = fontStyle + Font.BOLD;
      if (jchkItalic.isSelected())
        fontStyle = fontStyle + Font.ITALIC;
    
      // Set font for the message
      messagePanel.setFont(new Font(fontName, fontStyle, fontSize));
    }
    }         
    public class MessagePanel extends JPanel {
    private String message = "Welcome to Java";
    
    private int xCoordinate = 206; //x coordinate where message is displayed
    private int yCoordinate = 29; //y coordinate where message is displayed
    private boolean centered;//indicate whether message is displayed in the
    center
    private int interval = 10; // interval for moving message left/right
    
    public MessagePanel() {
    }
    
    public MessagePanel(String message) {
        this.message = message;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
        repaint();
    }
    
    public int getXCoordinate() {
        return xCoordinate;
    }
    
    public void setXCoordinate(int x) {
        xCoordinate = x;
    }
    
    public int getYCoordinate() {
        return yCoordinate;
    }
    
    public void setYCoordinate(int y) {
        yCoordinate = y;
    }
    
    public boolean isCentered() {
        return centered;
    }
    
    public void setCentered(boolean centered) {
        this.centered = centered;
        repaint();
    }
    
    public int getInterval() {
        return interval;
    }
    
    public void setInterval(int interval) {
        this.interval = interval;
        repaint();
    }
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        if (centered) {
            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 character in the baseline
            xCoordinate = getWidth() / 2 - stringWidth / 2;
            yCoordinate = getHeight() / 2 - stringAscent / 2;
        }
      
        g.drawString(message, xCoordinate, yCoordinate);
       }
       
       public void moveLeft() {
        xCoordinate -= interval;
        repaint();
       }
      
       public void moveRight() {
        xCoordinate += interval;
        repaint();
       }
       
       //@override
       public Dimension getPreferredSize() {
        return new Dimension(200, 30);
       }  
       }
       }
我在这里检查了半打关于同一个错误的帖子,但我不能 找出哪里出了问题。 我已经创建了许多不同的文件,但我多次遇到相同的错误。
请确认我的错误,这里是GUIenter代码的编码

,因为你的开始和结束括号{和}是混乱的

public class MessagePanel extends JPanel {
这是在一个方法中,因此不起作用。在前面添加一个}。在文件末尾,您至少还需要一个附加的}

我建议将每个类放在自己的文件中。这是最清晰的方法,尤其是对于初学者

你有两个重复的方法,左移,右移。你应该检查你的变量:


jbtleft与jbtleft不一样

我在自己的文件中添加了所有内容,但什么是重复文件?是不是我应该从编码重复文件中删除它?对不起,我不知道你们在说什么。我已经改进了我的编码,但我得到了一个错误,如注释:Question2.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:未选中以获取详细信息。??这是什么意思?看,我已经尽力解决这个编码,但不能,看我的编码,我已经编辑到正确的一个。但当我运行多次时,我会遇到相同的错误,请注意:Question2.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:未选中以获取详细信息。你能帮我修一下吗?我该怎么办?我试了很多次,但我不明白你发送给@Florian Schaetz的链接