需要加一个"public static void main(String[] args)",
Need to add a "public static void main(String[] args)",
我正在尝试在 Eclipse 中编写代码,在单击时更改 MyButton 上的颜色和文本。
编辑:得到了一些帮助,错误问题得到了解决。我现在知道代码需要 "public static void main(String[] args)" 应该放在哪里?
在我尝试 运行 这段代码后,我得到了错误 "The selection can't be launched, and there are no recent launches"。
然而,我用谷歌搜索了一下,发现这个错误可能是 eclipse 中的一个常见问题。我是,虽然是 java 的新手,并且假设如果我的代码有问题而不是 eclipse 讨厌我会更合乎逻辑。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Makke extends JFrame implements ActionListener{
private JButton MyButton;
public Makke(){
setLayout(null);
setSize(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyButton = new JButton("Tryck För Blå!");
MyButton.setBackground(Color.YELLOW);
MyButton.setBounds(100, 190, 60, 30);
MyButton.addActionListener(this);
add (MyButton);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == MyButton) {
MyButton = new JButton("Tryck För Gul!");
MyButton.setBackground(Color.BLUE);
}
}
}
翻译:
代码里有些瑞典语 "Tryck för blå" = "Click to get blue", "Tryck för gul" = "Click to get yellow" ^^
您每次点击都在创建一个新按钮,但没有向该新对象添加点击侦听器...
我的建议是这样做(或者如果你转向 javaFX 可能更好)
JButton myButton = new JButton("Text Button");
myButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
myButton.setText("Tryck För Gul!");
myButton.setBackground(Color.BLUE);
}
}
1.You 应将 setVisible( boolean b)
设置为 true
。
根据参数 b
的值显示或隐藏此 Window。
setVisible(true);
添加这一行应该在Makke
class.
的构造函数中
2.YouractionPerformed(.)
方法
if (e.getSource() == MyButton) {
//MyButton = new JButton("Tryck För Gul!"); remove this line
//otherwise every time new button object will be created.
MyButton.setText("Tryck För Gul!");//to change the button text
MyButton.setBackground(Color.BLUE);
}
3.Need 运行 您的应用的主要方法。创建一个 class 包含 main(-) 到 运行 您的应用程序,在 Makke
class.
的创建对象中
public class Main {
public static void main(String []args)throws Exception {
new Makke();//now your window will be appeared.
}
}
Read - Why main(-) method needed
我正在尝试在 Eclipse 中编写代码,在单击时更改 MyButton 上的颜色和文本。
编辑:得到了一些帮助,错误问题得到了解决。我现在知道代码需要 "public static void main(String[] args)" 应该放在哪里?
在我尝试 运行 这段代码后,我得到了错误 "The selection can't be launched, and there are no recent launches"。 然而,我用谷歌搜索了一下,发现这个错误可能是 eclipse 中的一个常见问题。我是,虽然是 java 的新手,并且假设如果我的代码有问题而不是 eclipse 讨厌我会更合乎逻辑。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Makke extends JFrame implements ActionListener{
private JButton MyButton;
public Makke(){
setLayout(null);
setSize(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyButton = new JButton("Tryck För Blå!");
MyButton.setBackground(Color.YELLOW);
MyButton.setBounds(100, 190, 60, 30);
MyButton.addActionListener(this);
add (MyButton);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == MyButton) {
MyButton = new JButton("Tryck För Gul!");
MyButton.setBackground(Color.BLUE);
}
}
}
翻译: 代码里有些瑞典语 "Tryck för blå" = "Click to get blue", "Tryck för gul" = "Click to get yellow" ^^
您每次点击都在创建一个新按钮,但没有向该新对象添加点击侦听器... 我的建议是这样做(或者如果你转向 javaFX 可能更好)
JButton myButton = new JButton("Text Button");
myButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
myButton.setText("Tryck För Gul!");
myButton.setBackground(Color.BLUE);
}
}
1.You 应将 setVisible( boolean b)
设置为 true
。
根据参数 b
的值显示或隐藏此 Window。
setVisible(true);
添加这一行应该在Makke
class.
2.YouractionPerformed(.)
方法
if (e.getSource() == MyButton) {
//MyButton = new JButton("Tryck För Gul!"); remove this line
//otherwise every time new button object will be created.
MyButton.setText("Tryck För Gul!");//to change the button text
MyButton.setBackground(Color.BLUE);
}
3.Need 运行 您的应用的主要方法。创建一个 class 包含 main(-) 到 运行 您的应用程序,在 Makke
class.
public class Main {
public static void main(String []args)throws Exception {
new Makke();//now your window will be appeared.
}
}
Read - Why main(-) method needed