Java新手 求教

怎么把我上面的程序 在我下面做好的框架中显示使用
2025-02-25 14:11:05
推荐回答(1个)
回答1:

两者不能结合使用,我直接给你一个,现写的!祝大家都快乐!
public class LoveFrame extends JFrame {

JTextField text = new JTextField();
JButton button = new JButton("惊喜一刻");
JLabel show = new JLabel();
public LoveFrame(){
this.setTitle("七夕节快乐");
this.setBounds(300, 100, 450, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("请输入你的名字");
button.setBounds(270, 50, 100, 30);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if((JButton)e.getSource() == button){
String temp = text.getText();
if("人名".equals(temp)){
show.setText("亲爱的"+temp+",七夕节快乐!爱你!");
show.setForeground(Color.red);
show.setVisible(true);
}else{
show.setText("自己的名字都输错,笨死啦。。。!");
show.setVisible(true);
}
}

}
});
this.setLayout(null);
label.setBounds(50, 50, 100, 30);
text.setBounds(160, 50, 100, 30);
show.setBounds(50, 180, 350, 50);
show.setFont(new Font(Font.DIALOG,5,20));
show.setVisible(false);
this.add(show);
this.add(button);
this.add(label);
this.add(text);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new LoveFrame();
}

}