java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码,然后自己再根据需要修改,如:
package mainFrame;
import javaawtEventQueue;
import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javaxswingImageIcon;
import javaxswingJButton;
import javaxswingJCheckBox;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJPasswordField;
import javaxswingJTextField;
import javaxswingSwingConstants;
import javaxswingUIManager;
import javaxswingUnsupportedLookAndFeelException;
import javaxswingborderEmptyBorder;
public class Mian_login extends JFrame {
private JPanel contentPane;
private JTextField text_LoginName;
private JPasswordField Login_password;
/
Launch the application
/
public static void main(String[] args) {
EventQueueinvokeLater(new Runnable() {
@Override
public void run() {
try {
Mian_login frame = new Mian_login();
framesetVisible(true);
} catch (Exception e) {
eprintStackTrace();
}
}
});
}
/
Create the frame
/
public Mian_login() {
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setBounds(500, 200, 443, 300);
setResizable(false);
setTitle("登 录");
/获取系统按钮样式/
String lookAndFeel = UIManagergetSystemLookAndFeelClassName();
try {
UIManagersetLookAndFeel(lookAndFeel);
} catch (ClassNotFoundException e1) {
e1printStackTrace();
} catch (InstantiationException e1) {
e1printStackTrace();
} catch (IllegalAccessException e1) {
e1printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1printStackTrace();
}
contentPane = new JPanel();
contentPanesetBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPanesetLayout(null);
JPanel panel = new JPanel();
panelsetOpaque(false);
panelsetBounds(0, 0, 434, 272);
contentPaneadd(panel);
panelsetLayout(null);
JButton btn_Login = new JButton("\u767B\u5F55");
btn_LoginaddMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
btn_LoginsetBounds(88, 195, 70, 23);
paneladd(btn_Login);
JButton btn_cancel = new JButton("\u53D6\u6D88");
btn_canceladdMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
dispose();
}
});
btn_cancelsetBounds(268, 195, 70, 23);
paneladd(btn_cancel);
JLabel lblNewLabel_name = new JLabel("\u7528\u6237\u540D");
lblNewLabel_namesetHorizontalAlignment(SwingConstantsCENTER);
lblNewLabel_namesetOpaque(true);
lblNewLabel_namesetBounds(88, 48, 70, 23);
paneladd(lblNewLabel_name);
JLabel lblNewLabel_passwd = new JLabel("\u5BC6\u7801");
lblNewLabel_passwdsetHorizontalAlignment(SwingConstantsCENTER);
lblNewLabel_passwdsetOpaque(true);
lblNewLabel_passwdsetBounds(88, 102, 70, 23);
paneladd(lblNewLabel_passwd);
JCheckBox chckbx_remember = new JCheckBox("\u8BB0\u4F4F\u5BC6\u7801");
chckbx_remembersetBounds(102, 150, 84, 23);
paneladd(chckbx_remember);
text_LoginName = new JTextField();
text_LoginNamesetBounds(182, 48, 156, 23);
paneladd(text_LoginName);
text_LoginNamesetColumns(10);
Login_password = new JPasswordField();
Login_passwordsetBounds(182, 102, 156, 23);
paneladd(Login_password);
JCheckBox chckbx_AutoLogin = new JCheckBox("\u81EA\u52A8\u767B\u5F55");
chckbx_AutoLoginsetBounds(233, 150, 84, 23);
paneladd(chckbx_AutoLogin);
JLabel Label_background = new JLabel("");
Label_backgroundsetIcon(new ImageIcon("E:\\JAVA_workplace\\0002-\u754C\u9762\u8BBE\u8BA1\\images\\background3jpg"));
Label_backgroundsetBounds(0, 0, 437, 272);
contentPaneadd(Label_background);
}
}
如果您正在使用 Python 中的 `ipywidgets` 库,可以通过在 `interact` 函数中设置 `manual=True` 来初始化 `interact` 函数并在用户单击按钮时显示d出框。以下是一个示例代码:```python
import ipywidgets as widgets
def calculate_result(x, y):
# 执行计算
result = x + y
return result
# 创建一个文本框和一个按钮
text_box_x = widgetsFloatText(description='X:')
text_box_y = widgetsFloatText(description='Y:')
button = widgetsButton(description='计算')
# 创建一个输出框
output = widgetsOutput()
# 定义点击按钮时要执行的函数
def on_button_click(button):
with output:
result = calculate_result(text_box_xvalue, text_box_yvalue)
print(f"结果为:{result}")
# 在按钮上注册点击事件
buttonon_click(on_button_click)
# 创建一个d出框,包含文本框、按钮和输出框
box = widgetsVBox([text_box_x, text_box_y, button, output])
# 在d出框上调用 interact 函数
widgetsinteract(lambda x: box, x=True, manual=True)
```
在上面的示例中,`interact` 函数的第一个参数是一个函数,它返回一个d出框。`manual=True` 表示要手动触发d出框的显示。在d出框中,我们创建了两个文本框和一个按钮,以便用户输入要计算的值,然后单击按钮来执行计算。计算结果将打印到d出框的输出框中。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)