java中的窗体设置不可放大_Java窗口运行不显示内容,放大一下才显示内容。

求助:Java窗口运行不显示内容,放大一下才显示内容。大佬们帮忙看看这是我的代码问题还是其他啥问题?

436e1eefbbe8a1a88a536dc6950cfc51.png

1e1e47f5e437926898e2f61129cb7090.png

8499ce184eec1ab5367a9f8252302b06.png

import java.awt.*;

import javax.swing.*;

public class SchoolWork extends JFrame {

public static void main(String args[]) {

JFrame frame = new JFrame("常用组件") ;

JCheckBox checkBox1,checkBox2,checkBox3,checkBox4;

JRadioButton radioM,radioF;

JComboBoxcomBox;

JTextField text;

JLabel textLab1,textLab2,textLab3,textLab4,textLab5,textLab6;

JButton but;

ButtonGroup group;

JTextArea jText;

frame.setVisible(true);

frame.setBounds(100,100,380,260);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new FlowLayout(FlowLayout.CENTER,30,20));

JPanel panel = new JPanel();

frame.setContentPane(panel);

text = new JTextField(15) ;

textLab1 = new JLabel("文本框:") ;

frame.setLayout(new FlowLayout());

panel.add(textLab1);

panel.add(text);

textLab2 = new JLabel("按钮:") ;

panel.add(textLab2);

but = new JButton("按钮");

Font fnt = new Font("Serief",Font.BOLD,12);

but.setFont(fnt) ;

panel.add(but);

textLab3 = new JLabel("选择框:");

panel.add(textLab3);

checkBox1 = new JCheckBox("喜欢音乐");

checkBox2 = new JCheckBox("喜欢足球");

checkBox3 = new JCheckBox("喜欢游泳");

panel.add(checkBox1);

panel.add(checkBox2);

panel.add(checkBox3);

textLab4 = new JLabel("单选按钮:");

panel.add(textLab4);

radioM = new JRadioButton("男");

radioF = new JRadioButton("女");

group = new ButtonGroup();

group.add(radioF);

group.add(radioM);

panel.add(radioF);

panel.add(radioM);

textLab5 = new JLabel("下拉列表:");

panel.add(textLab5);

comBox = new JComboBox();

comBox.addItem("音乐天地");

comBox.addItem("武术天地");

panel.add(comBox);

textLab6 = new JLabel("文本区:");

panel.add(textLab6);

jText = new JTextArea(5,20);

panel.add(jText);

}

}

回答

代码最后调用下

frame.repaint();

frame.setVisible(true);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Java窗体显示MySQL数据,可以使用Java Swing提供的JTable组件来实现。以下是实现步骤: 1. 导入MySQL数据库驱动包 在项目引入MySQL数据库的驱动包,可以使用JDBC连接MySQL数据库。 2. 连接MySQL数据库 使用JDBC连接MySQL数据库,创建一个Connection对象。 3. 查询MySQL数据数据 使用JDBC执行SQL语句,查询需要显示数据,并将结果保存在ResultSet对象。 4. 将查询结果转换为二维数组 遍历ResultSet对象,将查询结果转换为一个二维数组。 5. 创建JTable组件 使用JTable组件显示查询结果,创建一个JTable对象并将二维数组作为参数传入。 6. 将JTable组件添加到窗体 使用Java Swing提供的容器组件,如JFrame、JPanel等,将JTable组件添加到窗体。 以下是示例代码: ```java import java.sql.*; import javax.swing.*; import java.awt.*; public class DisplayTable extends JFrame { private JTable table; public DisplayTable() { setTitle("Display Table"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 连接MySQL数据库 try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password"); // 查询MySQL数据数据 Statement stmt = con.createStatement(); String query = "SELECT * FROM mytable"; ResultSet rs = stmt.executeQuery(query); // 将查询结果转换为二维数组 int rowCount = 0; while (rs.next()) { rowCount++; } rs.beforeFirst(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Object[][] data = new Object[rowCount][columnCount]; int i = 0; while (rs.next()) { for (int j = 0; j < columnCount; j++) { data[i][j] = rs.getObject(j + 1); } i++; } // 创建JTable组件 table = new JTable(data, getColumnNames(rsmd)); // 将JTable组件添加到窗体 JScrollPane scrollPane = new JScrollPane(table); getContentPane().add(scrollPane, BorderLayout.CENTER); // 关闭MySQL数据库连接 rs.close(); stmt.close(); con.close(); } catch (Exception ex) { ex.printStackTrace(); } } // 获取查询结果的列名 private String[] getColumnNames(ResultSetMetaData rsmd) throws SQLException { int columnCount = rsmd.getColumnCount(); String[] columnNames = new String[columnCount]; for (int i = 1; i <= columnCount; i++) { columnNames[i - 1] = rsmd.getColumnName(i); } return columnNames; } public static void main(String[] args) { DisplayTable frame = new DisplayTable(); frame.setVisible(true); } } ``` 在以上示例代码,需要将“mydatabase”和“mytable”替换为实际使用的数据库和表名,将“root”和“password”替换为实际的MySQL数据库用户名和密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值