我遇到了一個問題,我真的不知道如何在Java Swing GUI中創建一個功能按鈕(我想這應該稱爲它)。我創建了一個打印語句來檢查我的按鈕是否工作,並且它不起作用。這裏是我的代碼。Java按鈕不起作用
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
/**
* Create a JFrame to hold our beautiful drawings.
*/
public class Jan1UI implements ActionListener
{
/**
* Creates a JFrame and adds our drawings
*
* @param args not used
*/
static JFrame frame = new JFrame();
static JButton nextBut = new JButton("NEXT");
static NextDayComponents nextDaycomponent = new NextDayComponents();
public static void main(String[] args)
{
//Set up the JFrame
nextBut.setBounds(860, 540, 100, 40);
/*nextBut.setOpaque(false);
nextBut.setContentAreaFilled(false);
nextBut.setBorderPainted(false);
*/
frame.setSize(1920, 1080);
frame.setTitle("Jan1UI demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
frame.setVisible(true);
frame.add(nextBut);
frame.add(nextDaycomponent);
}
public void actionPerformed(ActionEvent e)
{
JButton b = (JButton)e.getSource();
if (b == nextBut)
{
System.out.println("ok");
}
}
}
/*static class Butt implements ActionListener
{
}*/
它看起來像是有效的,除了button但我想我可以自己解決這個問題,非常感謝你的答覆,並且給我提供了完整的解決方案。 –
我從示例中刪除了'NextDayComponents',因爲沒有該代碼 - 但是假設將填寫主要區域 – lostbard