java期末考试题

发布网友 发布时间:2022-04-22 06:44

我来回答

1个回答

热心网友 时间:2022-06-16 18:36

单击出现你好的我倒还知道点
package example;

import java.awt.BorderLayout;

public class Switch extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Switch frame = new Switch();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Switch() {
final String string = "你好";
final String string2 = "再见";
setTitle("\u5207\u6362\u6807\u7B7E\u5185\u5BB9");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
final JLabel label = new JLabel(string);
label.setFont(new Font("宋体",Font.BOLD,25));
JButton button = new JButton("\u786E\u5B9A");
button.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (label.getText().equals(string)) {
label.setText(string2);
}
else if (label.getText().equals(string2)) {
label.setText(string);
}
}

});
GroupLayout groupLayout = new GroupLayout(contentPane);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(153, 153, 153)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button)
.addComponent(label))
.addContainerGap(222, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(80, 80, 80)
.addComponent(label)
.addGap(38, 38, 38)
.addComponent(button)
.addContainerGap(100, Short.MAX_VALUE))
);
contentPane.setLayout(groupLayout);
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com