java中的JComboBox问题
按照以下的代码一按确定,JComboBox就自动变宽了,能不能固定JComboBox的宽度就算按了确定也不会改变呢?importjava.awt.*;importjava...
按照以下的代码一按确定,JComboBox就自动变宽了,能不能固定JComboBox的宽度就算按了确定也不会改变呢?
import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;
public class AAA extends JFrame{ public AAA(){ JPanel[] panel = new JPanel[2]; panel[0] = new JPanel(); panel[1] = new JPanel(); final JComboBox combo = new JComboBox(); JButton button = new JButton("确定"); panel[0].add(combo); panel[1].add(button); add(panel[0], BorderLayout.CENTER); add(panel[1], BorderLayout.SOUTH); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ Connection conn = getConnection("192.168.10.50", "master", "sa", "1719851129"); PreparedStatement stat = conn.prepareStatement("select getdate()"); ResultSet rs = stat.executeQuery(); while(rs.next()){ combo.addItem(rs.getString(1)); } } catch(SQLException ex){} } }); } public static Connection getConnection(String ip, String name, String username, String password) throws SQLException{ String drivers = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; System.setProperty("jdbc.drivers", drivers); String url = "jdbc:sqlserver://" + ip + ":1433;databaseName=" + name; return DriverManager.getConnection(url, username, password); } public static void main(String[] args){ AAA a = new AAA(); a.setLocationByPlatform(true); a.setVisible(true); }} 展开
import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;
public class AAA extends JFrame{ public AAA(){ JPanel[] panel = new JPanel[2]; panel[0] = new JPanel(); panel[1] = new JPanel(); final JComboBox combo = new JComboBox(); JButton button = new JButton("确定"); panel[0].add(combo); panel[1].add(button); add(panel[0], BorderLayout.CENTER); add(panel[1], BorderLayout.SOUTH); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ Connection conn = getConnection("192.168.10.50", "master", "sa", "1719851129"); PreparedStatement stat = conn.prepareStatement("select getdate()"); ResultSet rs = stat.executeQuery(); while(rs.next()){ combo.addItem(rs.getString(1)); } } catch(SQLException ex){} } }); } public static Connection getConnection(String ip, String name, String username, String password) throws SQLException{ String drivers = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; System.setProperty("jdbc.drivers", drivers); String url = "jdbc:sqlserver://" + ip + ":1433;databaseName=" + name; return DriverManager.getConnection(url, username, password); } public static void main(String[] args){ AAA a = new AAA(); a.setLocationByPlatform(true); a.setVisible(true); }} 展开
3个回答
展开全部
追问
1、如果不设布局,那怎么把JComBox进行排版呢?
2、如果不设布局,那么点击确定的时候,点击JComboBox的三角型弹出下拉列表时,它只显示一部分时间,但是我想看到全部,又怎么设置呢?
展开全部
so easy
setBounds
jComboBox1.setBounds(150, 104, 90, 24);
but your layout must be null
if you don't know how do components work ,you can take this into your eclipse
have fun:)
guy
追问
1、如果不设布局,那怎么把JComBox进行排版呢?
2、如果不设布局,那么点击确定的时候,点击JComboBox的三角型弹出下拉列表时,它只显示一部分时间,但是我想看到全部,又怎么设置呢?
追答
just setBounds done
setBounds(x,y,width,height);
x: your combox horizontal ordinate about frame
y: your combox vertical coordinates about frame
width: your combox's width
height: your combox's height
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JComboBox combo = new JComboBox();
combo.setSize(100, 50); 加上这句
combo.setSize(100, 50); 加上这句
追问
改了也没用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询