package com.nono.groupproject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class LoginGUI extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
LoginGUI loginGUI = new LoginGUI();
loginGUI.createAndShowGUI();
});
}
public void createAndShowGUI() {
setTitle("Login");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 1, 20, 20));
JLabel lblLoginAs = new JLabel("Login as:");
lblLoginAs.setFont(new Font("Arial", Font.PLAIN, 20));
panel.add(lblLoginAs);
JButton btnAdmin = createLoginButton("Admin");
panel.add(btnAdmin);
JButton btnUser = createLoginButton("User");
panel.add(btnUser);
setContentPane(panel);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int) (screenSize.getWidth() * 0.4);
int screenHeight = (int) (screenSize.getHeight() * 0.4);
setPreferredSize(new Dimension(screenWidth, screenHeight));
pack();
setLocationRelativeTo(null);
setVisible(true);
有人可以帮助我输入什么编码来在按钮管理员和用户中添加颜色背景吗?
另外,你能检查一下我的编码是否正确吗?给我一些建议和答案谢谢。
setBackground()
方法,传递 java.awt.Color。
来自文档,对于
setBackground()
:
设置该组件的背景颜色。仅当组件不透明时才使用背景颜色,并且仅由 JComponent 或 ComponentUI 实现的子类使用。 JComponent 的直接子类必须重写 PaintComponent 才能实现此属性。
示例:
btnAdmin.setBackground(Color.BLUE);