50,550
社区成员
发帖
与我相关
我的任务
分享
package com.car.test;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import javax.swing.*;
import javax.swing.border.*;
import com.car.dao.Dao;
import com.car.model.TbVote;
import com.car.util.*;
public class ComBoBoxTest{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new ComBoBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class ComBoBoxFrame extends JFrame
{
public ComBoBoxFrame()
{
setTitle("ComBoBoxFrameTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JPanel panel = new ComBoBox();
add(panel);
}
private static final int DEFAULT_WIDTH = 750;
private static final int DEFAULT_HEIGHT = 400;
private static final long serialVersionUID = 1L;
}
//主要内容
class ComBoBox extends JPanel {
public ComBoBox()
{
super();
setSize(WIDTH, HEIGHT);
setBackground(new Color(160, 160, 52));
JPanel inPanel = new InPanel();
JScrollPane scrollPane = new JScrollPane(inPanel);
scrollPane.setPreferredSize(new Dimension(700, 700));
scrollPane.setBorder(BorderFactory.createEmptyBorder());
add(scrollPane);
}
private class InPanel extends JPanel
{
public InPanel()
{
super();
setLayout(null);
setOpaque(false);
setSize(700, 360);
card = new CardLayout();
cardPanel = new JPanel(card);
cardPanel.setOpaque(false);
cardPanel.setBounds(0, 0, 700, 350);
JPanel firstPanel = new FirstPanel();
firstPanel.setName("first");
cardPanel.add(firstPanel, "first");
add(cardPanel);
card.show(cardPanel, "first");
}
private class FirstPanel extends JPanel
{
public FirstPanel()
{
super();
setLayout(null);
setSize(700, 350);
setBackground(new Color(160, 160, 52));
setContent();
ComBoBox.setContent();//初始化topicCombo的选项 方法在最下面 是静态方法
}
private void setContent()
{
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setOpaque(false);
panel.setBounds(25, 5, 650, 355);
Font font = new MyFont().getFont();
topicLabel = new JLabel("主题:", SwingConstants.RIGHT);
topicLabel.setFont(font);
topicLabel.setBounds(0, 0, 75, 25);
topicField = new JTextField(25);
topicField.setBounds(80, 0, 205, 25);
topicInsertButton = new JButton("插入");
topicInsertButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(topicField.getText() != null && topicField.getText() != "")
{
TbVote vote = new TbVote();
// vote.setAdmiId(LoginFrame.user.getId());
vote.setContentType("乘车投票");
vote.setDurationTime(3);
vote.setEstablishTime(ChangeDateTime.dateToStrLong(new Date()));
vote.setParticipantNumber(0);
vote.setPicture(null);
vote.setTitle(topicField.getText().trim());
if(Dao.addVote(vote))//往数据库里面写选项 接下来是想同时更新topicCombo 让其选项也随着增加新选项
JOptionPane.showMessageDialog(null, "插入成功,请尽快写选项");
//下面的方法我都试过 可以插入 但是topicCombo内的选项没变过
// topicComboModel.removeAllElements();
// topicComboModel.addElement(topicField.getText().trim());
// topicCombo.setModel(topicComboModel);
// topicComboModel.removeAllElements();
// topicComboModel.addElement(topicField.getText().trim());
// topicComboModel.insertElementAt(topicField.getText().trim(), 1);
// topicCombo.setModel(topicComboModel);
// topicCombo.setSelectedItem(topicField.getText().trim());
// topicCombo.addItem(topicField.getText().trim());
// ComBoBox.setContent();
}
}
});
topicInsertButton.setFont(font);
topicInsertButton.setFocusPainted(false);
topicInsertButton.setBackground(Color.GREEN);
topicInsertButton.setBounds(290, 0, 75, 25);
topicCombo = new JComboBox<String>();
topicComboModel = new DefaultComboBoxModel<String>(new String[]{"请选择"});
topicCombo.setModel(topicComboModel);
topicCombo.setFont(font);
topicCombo.setFocusable(false);
topicCombo.setBounds(370, 0, 200, 25);
topicDeleteButton = new JButton("删除");
topicDeleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
});
topicDeleteButton.setFont(font);
topicDeleteButton.setFocusPainted(false);
topicDeleteButton.setBackground(Color.RED);
topicDeleteButton.setBounds(575, 0, 75, 25);
JPanel questionPanel = new JPanel();
Font titleFont = new MyFont().setSize(16).getFont();
questionPanel.setLayout(null);
questionPanel.setOpaque(false);
questionPanel.setBounds(0, 30, 650, 140);
questionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "问题", TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION, titleFont));
topicOkLabel = new JLabel("题目:", SwingConstants.RIGHT);
topicOkLabel.setFont(font);
topicOkLabel.setBounds(20, 15, 75, 25);
topicOkCombo = new JComboBox<String>();
topicOkComboModel = new DefaultComboBoxModel<String>(new String[]{"请选择"});
topicOkCombo.setModel(topicOkComboModel);
topicOkCombo.setFont(font);
topicOkCombo.setFocusable(false);
topicOkCombo.setBounds(100, 15, 300, 25);
topicOkCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
});
firstQuestionLabel = new JLabel("A选项:", SwingConstants.RIGHT);
firstQuestionLabel.setFont(font);
firstQuestionLabel.setBounds(5, 45, 60, 25);
firstQuestionField = new JTextField(25);
firstQuestionField.setBounds(70, 45, 250, 25);
secondQuestionLabel = new JLabel("B选项:", SwingConstants.RIGHT);
secondQuestionLabel.setFont(font);
secondQuestionLabel.setBounds(325, 45, 60, 25);
secondQuestionField = new JTextField(25);
secondQuestionField.setBounds(385, 45, 250, 25);
thirdQuestionLabel = new JLabel("C选项:", SwingConstants.RIGHT);
thirdQuestionLabel.setFont(font);
thirdQuestionLabel.setBounds(5, 75, 60, 25);
thirdQuestionField = new JTextField(25);
thirdQuestionField.setBounds(70, 75, 250, 25);
fourthQuestionLabel = new JLabel("D选项:", SwingConstants.RIGHT);
fourthQuestionLabel.setFont(font);
fourthQuestionLabel.setBounds(325, 75, 60, 25);
fourthQuestionField = new JTextField(25);
fourthQuestionField.setBounds(385, 75, 250, 25);
questionConfirmButton = new JButton("确定");
questionConfirmButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
});
questionConfirmButton.setFont(font);
questionConfirmButton.setFocusPainted(false);
questionConfirmButton.setBackground(Color.GREEN);
questionConfirmButton.setBounds(255, 105, 75, 25);
questionPanel.add(topicOkLabel);
questionPanel.add(topicOkCombo);
questionPanel.add(firstQuestionLabel);
questionPanel.add(firstQuestionField);
questionPanel.add(secondQuestionLabel);
questionPanel.add(secondQuestionField);
questionPanel.add(thirdQuestionLabel);
questionPanel.add(thirdQuestionField);
questionPanel.add(fourthQuestionLabel);
questionPanel.add(fourthQuestionField);
questionPanel.add(questionConfirmButton);
panel.add(topicLabel);
panel.add(topicField);
panel.add(topicInsertButton);
panel.add(topicCombo);
panel.add(topicDeleteButton);
panel.add(questionPanel);
add(panel);
}
private JLabel topicLabel;
private JLabel topicOkLabel;
private JLabel firstQuestionLabel;
private JLabel secondQuestionLabel;
private JLabel thirdQuestionLabel;
private JLabel fourthQuestionLabel;
private JTextField topicField;
private JTextField firstQuestionField;
private JTextField secondQuestionField;
private JTextField thirdQuestionField;
private JTextField fourthQuestionField;
private JButton topicInsertButton;
private JButton topicDeleteButton;
private JButton questionConfirmButton;
private static final long serialVersionUID = 1L;
}
private static final long serialVersionUID = 1L;
}
public static void setContent()
{
List<TbVote> votes;
votes = Dao.getVotes("乘车投票");
if(votes.size() > 0)
{
ListIterator<TbVote> voteIter = votes.listIterator();
String[] voteStrs = new String[votes.size() + 1];
int i = 1;
while(voteIter.hasNext())
{
voteStrs[i] = voteIter.next().getTitle();
i++;
}
voteStrs[0] = "请选择";
topicComboModel = new DefaultComboBoxModel<String>(voteStrs);
topicOkComboModel = new DefaultComboBoxModel<String>(voteStrs);
}
topicCombo.setModel(topicComboModel);
topicOkCombo.setModel(topicOkComboModel);
}
public static JComboBox<String> topicCombo;
public static JComboBox<String> topicOkCombo;
public static DefaultComboBoxModel<String> topicComboModel;
public static DefaultComboBoxModel<String> topicOkComboModel;
public static CardLayout card;
public static JPanel cardPanel;
private static final int WIDTH = 700;
private static final int HEIGHT = 360;
private static final long serialVersionUID = 1L;
}