gpt4 book ai didi

java - 组合框未显示

转载 作者:行者123 更新时间:2023-12-02 05:50:48 25 4
gpt4 key购买 nike

所以,我的要求是使用 Java 中的 JTable 显示数据库中的记录。我提到了this link 。但错误是,当我在 Eclipse 中运行它时,我看不到理想情况下应该出现的组合框。如何解决?

 import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class DisplayEmpData extends JFrame implements ActionListener {

JFrame frame1;
JLabel l0, l1, l2;
JComboBox c1;
JButton b1;
Connection con;
ResultSet rs, rs1;
Statement st, st1;
PreparedStatement pst;
String ids;
static JTable table;
String[] columnNames = {"User name", "Email", "Password", "Country"};
String from;

DisplayEmpData() {

l0 = new JLabel("Fatching Employee Information");
l0.setForeground(Color.red);
l0.setFont(new Font("Serif", Font.BOLD, 20));
l1 = new JLabel("Select name");
b1 = new JButton("submit");

l0.setBounds(100, 50, 350, 40);
l1.setBounds(75, 110, 75, 20);
b1.setBounds(150, 150, 150, 20);
b1.addActionListener(this);

setTitle("Fetching Student Info From DataBase");
setLayout(null);
setVisible(true);
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

add(l0);
add(l1);;
add(b1);
try {

String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\users\\ppreeti\\emp_details.accdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
/*con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");*/
st = con.createStatement();
rs = st.executeQuery("select uname from emp");
Vector v = new Vector();
while (rs.next()) {
ids = rs.getString(1);
v.add(ids);
}
c1 = new JComboBox(v);
c1.setBounds(150, 110, 150, 20);

add(c1);
st.close();
rs.close();
} catch (Exception e) {
}
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
showTableData();
}

}

public void showTableData() {

frame1 = new JFrame("Database Search Result");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setLayout(new BorderLayout());
//TableModel tm = new TableModel();
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
//DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());
//table = new JTable(model);
table = new JTable();
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(table);
scroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
from = (String) c1.getSelectedItem();
//String textvalue = textbox.getText();
String uname = "";
String email = "";
String pass = "";
String cou = "";

try {
pst = con.prepareStatement("select * from emp where UNAME='" + from + "'");
ResultSet rs = pst.executeQuery();
int i = 0;
if (rs.next()) {
uname = rs.getString("uname");
email = rs.getString("umail");
pass = rs.getString("upass");
cou = rs.getString("ucountry");
model.addRow(new Object[]{uname, email, pass, cou});
i++;
}
if (i < 1) {
JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
}
if (i == 1) {
System.out.println(i + " Record Found");
} else {
System.out.println(i + " Records Found");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
frame1.add(scroll);
frame1.setVisible(true);
frame1.setSize(400, 300);
}

public static void main(String args[]) {
new DisplayEmpData();
}
}

最佳答案

首先,我的建议是在代码中为方法和字段使用更有意义的名称:现在对您来说可能看起来很清晰,但对于外部读者和您自己来说,几个月后代码的作用就赢了一点也不明显。

也就是说,您可能最好使用 LayoutManager,它可以确保正确的 Component 放置和平台上的可见性(正如 @Andrew Thompson 已经指出的那样)。

基本上来说,第一步是创建一个 JPanel 并将其添加到您的 JFrame 中:

JPanel framePanel;
framePanel = new JPanel();
this.add(framePanel);

JPanel() 构造函数中未指定任何参数时,布局将自动设置为 BorderLayout。此布局有 5 个可以放置组件的区域:北、南、西、东、中心。

您现在可以将其他 JPanel 添加到您的 framePanel 中,如下所示:

JPanel northPanel;
JPanel southPanel;
JPanel westPanel;
JPanel eastPanel;
JPanel centerPanel;

northPanel = new JPanel();
southPanel = new JPanel();
westPanel = new JPanel();
eastPanel = new JPanel();
centerPanel = new JPanel();

framePanel.add(northPanel, BorderLayout.NORTH);
framePanel.add(southPanel, BorderLayout.SOUTH);
framePanel.add(westPanel, BorderLayout.WEST);
framePanel.add(eastPanel, BorderLayout.EAST);
framePanel.add(centerPanel, BorderLayout.CENTER);

每个面板可能包含另一个 JPanel 或您的一个组件。 GridLayout 是可用于放置多个组件的最有用和基本的 LayoutManager 之一,它允许您指定网格大小(行数和列数):每次您添加一个组件,将其放置在从左上角开始的第一个空闲槽中。

如果您要向 northPanel 添加 3 个组件,您需要编写代码

northPanel.setLayout(new GridLayout(1, 3));

northPanel.add(component1);
northPanel.add(component2);
northPanel.add(component3);

这些只是示例,如果您搜索 LayoutManager,您可以在 Java API 中找到更多信息。只是一些快速提示:

  • 您可以直接在 JPanel 构造函数中指定 Layout:

    JPanel testPanel = new JPanel(new GridLayout(行、列));

  • 您可以通过在网格位置插入空 JPanel 来添加空格:

    JPanel whiteSpacePanel = new JPanel();   

    testPanel.add(component1);
    testPanel.add(whiteSpacePanel);
    testPanel.add(component2);
  • 您可以在 Layout 构造函数中指定 Component 之间的一些基本边框

    JPanel panelWithBorders = new JPanel(new GridLayout(rows, columns, xBorder, yBorder));

引用文献:

关于java - 组合框未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23557136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com