- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的错误
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);
}
}
}
我在这里查看了六篇关于相同错误的帖子,但我无法弄清楚出了什么问题。创建了许多不同的文件,但我反复遇到相同的错误。已经识别出我的错误,这是 GUI 的编码在此处输入代码
最佳答案
因为你的左括号和右括号 { 和 } 很困惑。
public class MessagePanel extends JPanel {
这是在方法内部,因此不起作用。在其前面添加一个 }。在文件末尾,您至少还需要一个额外的 } 。
我建议将每个类放在自己的文件中。这是最清晰的方法,特别是对于初学者来说。
那里有两个重复的方法(moveLeft、moveRight)。你应该检查你的变量:
jbtleft
与 jbtLeft
关于java - 为什么会出现这种情况以及如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31879498/
我正在创建一个 sql server 存储过程,它将输入作为逗号分隔的 productid 或选项“全部”。当用户传入逗号分隔的产品 ID 时,查询应输出所有产品 ID 的数据。我使用“IN”语句执行
我有一个自动生成的 Web 服务客户端。我有很多复杂的类,我必须对其进行模式匹配。现在我的结构如下所示: val response = client.getResponse response matc
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 7年前关闭。 Improve this
我需要正确的 tsql 语法来解决这个问题: Select * from table where var_A='10' select * from table where var_B='10' 何时使
我遇到了这个问题。每当我运行程序并在需要时键入字母 m 时,我的 if 语句都不会识别它。有人知道为什么吗?我已经这样做了一个小时,但没有结果。 #include #include #includ
我从数据库列名称“你有护照”创建了一个表,用户回答是或否我如何将 css 应用到这个动态工作的表。 table, th, td { border: 1px solid black;
我对 LocationListener 类的 onStatusChanged 有一些疑问。 它知道它可以呈现三种状态:AVAILABLE、TEMPORARILY_UNAVAILABLE 和 OUT_O
当引入新的异常类型时,我总是不确定如何正确地做到这一点。有共同约定吗?你怎么做呢? 我对您组织它们的范围感兴趣(将它们保留在它们所使用的单元中?在组件级别有一个单元?包级别?应用程序?) 这也会影响命
我使用以下内容创建了日期维度: https://www.codeproject.com/Articles/647950/Create-and-Populate-Date-Dimension-for-D
您好,我正在使用 Android 完全 Kiosk 浏览器,该浏览器使用 chrome Webview。但是 javascript 中的某些方法或函数无法正常工作,例如 window.print()。
我有以下代码: public void OpenFile(string FileName) { if (FileName == null)
获取索引越界异常 for (int recordData = 0; recordData < recordDataList.size(); recordData++) {
我使用它在发生错误时在登录中显示一条消息: × Invalid user or password
这是我的场景,我有一个异常列表,其中包含来自不同层次结构的任意异常,下面的代码快照将解释我需要做什么 private List connectionExceptions; try { // tryin
我尝试动态更新 Jtextpane 中的左缩进。但我不能!这是我尝试过的! DefaultStyledDocument document = (DefaultStyledDocument) textp
我不知道为什么这个异常不起作用...... import java.util.*; public class a { public static void main(String[] args
我目前在 case 中使用多个 when 时遇到问题。当我删除第二个当时,它就起作用了。这是什么问题? 报告的MYSQL错误为: #1064 - You have an error in your S
例如,我有一个表记录用户查看和下载文件的事件, file_id user activity 2 Tim view 1 Ron
这是一个非常愚蠢的问题,但我需要一点安慰/帮助。我有当前的“递归”情况: void add( int value ) { // do something ... // if ( conditi
我尝试使用以下代码在按钮数组上注册回调。但我似乎无法理解如何绑定(bind)回调中需要的字符串。任何建议将不胜感激! for (var i = 0; i < this.car_types.length
我是一名优秀的程序员,十分优秀!