2012-02-24 18 views
-2

我有一个滚动框,用户可以在其中选择选项。如果我希望用户选择在其他地方打印,我需要拨打什么?我已经有我想要的用户选择。录制用户选择java

results.setText(); 

括号里会有什么?

import javax.swing.*; 

import java.awt.*; 
import java.util.ArrayList; 

import java.awt.event.*; 

public class container implements ActionListener 
{ 
    JPanel panels; 
    Timer timer; 
    JTextField userTypingRegion; 
    JTextArea results; 
    JComboBox<Integer> ageEntries; 
    JComboBox<String> raceList; 

    public void init(Container pane) 
    { 
     JButton switcher = new JButton("Next/Back"); 
     switcher.addActionListener(this); 

     JPanel infoPage = new JPanel(); 
     infoPage.setBackground(Color.BLACK); 

     //CENTER   
     JPanel panelCenter = new JPanel(); 
     panelCenter.setBackground(Color.GRAY); 
     panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.Y_AXIS)); 
     infoPage.add(BorderLayout.CENTER, panelCenter); 


     ///Gender 
     JPanel genderSelection = new JPanel(); 
     JLabel gender = new JLabel("Gender:"); 
     JCheckBox checkGenderMale = new JCheckBox("Male"); 
     JCheckBox checkGenderFemale = new JCheckBox("Female"); 

     genderSelection.add(gender); 
     genderSelection.add(checkGenderMale); 
     genderSelection.add(checkGenderFemale); 

     panelCenter.add(genderSelection); 

     ///Age 
     JPanel ageSelection = new JPanel(); 
     JLabel age = new JLabel("Age:"); 

     ArrayList<Integer> ageList = new ArrayList<Integer> (); 

     for (int i = 1; i <= 100; ++i) 
     { 
      ageList.add(i); 
     } 

     DefaultComboBoxModel<Integer> modelAge = new DefaultComboBoxModel<Integer>(); 
     for (Integer i : ageList) 
     { 
      modelAge.addElement(i); 
     } 

     JComboBox<Integer> ageEntries = new JComboBox<Integer>(); 
     ageEntries.setModel(modelAge);     

     ageSelection.add(age); 
     ageSelection.add(ageEntries); 

     panelCenter.add(ageSelection);    

     ///Race 
     JPanel raceSelection = new JPanel(); 
     JLabel race = new JLabel("Race/Ethnicity:"); 
     String[] raceEntries = {"White", "Black", "Hispanic" 
             , "Asian/Pacific Islander" 
             , "Alaska Native/American Indian", "Confused"}; 
     JComboBox<String> raceList = new JComboBox<String>(raceEntries); 


     raceList.addActionListener(new transferInfo()); 

     raceSelection.add(race); 
     raceSelection.add(raceList); 

     panelCenter.add(raceSelection); 

     ///Question 1 
     JPanel firstQuestion = new JPanel(); 

     JLabel one = new JLabel("How often do you read?"); 
     String[] oneEntries = {"Always", "Often", "Sometimes" 
             , "Not Often", "What is reading?"}; 
     JComboBox<String> oneAnswer = new JComboBox<String>(oneEntries); 

     firstQuestion.add(one); 
     firstQuestion.add(oneAnswer); 

     panelCenter.add(firstQuestion); 

     ///Question 2 
     JPanel secondQuestion = new JPanel(); 

     JLabel two = new JLabel("Average time (in minutes) " + 
              "spent on the computer per day:"); 
     ArrayList<Integer> hourList = new ArrayList<Integer>(); 

     for (int z = 0; z <= 1440; z = z + 30) 
     { 
      hourList.add(z); 
     } 

     DefaultComboBoxModel<Integer> modelHour = new DefaultComboBoxModel<Integer>(); 
     for (Integer z : hourList) 
     { 
      modelHour.addElement(z); 
     } 

     JComboBox<Integer> hourEntries = new JComboBox<Integer>(); 
     hourEntries.setModel(modelHour); 

     secondQuestion.add(two); 
     secondQuestion.add(hourEntries); 

     panelCenter.add(secondQuestion); 

     ///Question 3 
     JPanel thirdQuestion = new JPanel(); 

     JLabel three = new JLabel("Favorite Subject"); 
     String[] threeEntries = {"English", "Math", "Science" 
            , "Foreign Languages", "History", "Hate them all"}; 
     JComboBox<String> threeAnswer = new JComboBox<String>(threeEntries); 

     thirdQuestion.add(three); 
     thirdQuestion.add(threeAnswer); 

     panelCenter.add(thirdQuestion); 

     ///Question 4 
     JPanel fourthQuestion = new JPanel(); 

     JLabel four = new JLabel("Average sleep (in minutes) per night:"); 

     ArrayList<Integer> sleepTimeList = new ArrayList<Integer>(); 

     for (int y = 0; y <= 1440; y = y + 30) 
     { 
      sleepTimeList.add(y); 
     } 

     DefaultComboBoxModel<Integer> modelSleepTime = new DefaultComboBoxModel<Integer>(); 
     for (Integer z : sleepTimeList) 
     { 
      modelSleepTime.addElement(z); 
     } 

     JComboBox<Integer> sleepTimeEntries = new JComboBox<Integer>(); 
     sleepTimeEntries.setModel(modelSleepTime); 

     fourthQuestion.add(four); 
     fourthQuestion.add(sleepTimeEntries); 

     panelCenter.add(fourthQuestion);     

     ///Question 5 
     JPanel fifthQuestion = new JPanel(); 

     JLabel five = new JLabel("I am..."); 
     String [] fiveEntries = {"Outgoing", "In the middle", "Shy"}; 
     JComboBox<String> fiveAnswer = new JComboBox<String>(fiveEntries); 

     fifthQuestion.add(five); 
     fifthQuestion.add(fiveAnswer); 

     panelCenter.add(fifthQuestion); 

     ///Question 6 
     JPanel sixthQuestion = new JPanel(); 

     JLabel six = new JLabel("I am..."); 
     String [] sixEntries = {"Adventurous", "In the middle", "Lazy"}; 
     JComboBox<String> sixAnswer = new JComboBox<String>(sixEntries); 

     sixthQuestion.add(six); 
     sixthQuestion.add(sixAnswer); 

     panelCenter.add(sixthQuestion); 

     ///Question 7 
     JPanel seventhQuestion = new JPanel(); 

     JLabel seven = new JLabel("I live in the..."); 
     String [] sevenEntries = {"City", "Suburb", "Country", "Narnia"}; 
     JComboBox<String> sevenAnswer = new JComboBox<String>(sevenEntries); 

     seventhQuestion.add(seven); 
     seventhQuestion.add(sevenAnswer); 

     panelCenter.add(seventhQuestion); 

     ///Question 8 
     JPanel eighthQuestion = new JPanel(); 

     JLabel eight = new JLabel("Please choose the painting you like."); 

     eighthQuestion.add(eight); 

     panelCenter.add(eighthQuestion); 

     ///Adding Picture 
     JPanel pictures = new JPanel(); 

     ImageIcon image = new ImageIcon("C:\\Users\\Kevin\\Desktop\\Left and Right.jpg"); 
     JLabel imageButton = new JLabel(); 

     imageButton.setIcon(image); 

     pictures.add(imageButton); 

     panelCenter.add(pictures); 

     ///Question 9 
     JPanel ninthQuestion = new JPanel(); 

     JCheckBox checkLeft = new JCheckBox("I like the left one!"); 
     JCheckBox checkRight = new JCheckBox("I like the right one!"); 

     ninthQuestion.add(checkLeft); 
     ninthQuestion.add(checkRight); 

     panelCenter.add(ninthQuestion); 

     ////Second Card 

     JPanel programFrame = new JPanel(); 
     programFrame.setBackground(Color.BLACK); 

     programFrame.setMinimumSize(new Dimension(200, 300)); 
     programFrame.setMaximumSize(new Dimension(800, 700)); 
     programFrame.setPreferredSize(new Dimension(500, 500)); 


    ///CENTER DATA COLLECTION REGION 
    JPanel dataCollectionRegion = new JPanel(); 

    dataCollectionRegion.setBackground(Color.LIGHT_GRAY); 
    dataCollectionRegion.setLayout(
         new BoxLayout(dataCollectionRegion, BoxLayout.Y_AXIS)); 
    programFrame.add(BorderLayout.CENTER, dataCollectionRegion); 

    ///South Region 
    JPanel southRegion = new JPanel(); 

    southRegion.setBackground(Color.BLACK); 
    southRegion.setLayout(new BoxLayout(southRegion, BoxLayout.Y_AXIS)); 
    programFrame.add(BorderLayout.NORTH, southRegion); 

    ///Data Components 

    JLabel sampleWriting = new JLabel("<html>7 Dear friends, let us love one another, for love comes from God. <br>Everyone who loves has been born of God and knows God. 8 Whoever <br>does not love does not know God, because God is love. 9 This is how <br>God showed his love among us: He sent his one and only Son into <br>the world that we might live through him. 10 This is love: not that we <br>loved God, but that he loved us and sent his Son as an atoning sacrifice <br> for our sins. 11 Dear friends, since God so loved us, we also ought <br>to love one another. 12 No one has ever seen God; but if we love one <br> another, God lives in us and his love is made complete in us. <br> 1 Everyone who believes that Jesus is the Christ is born of God, and everyone <br> who loves the father loves his child as well. 2 This is how we know <br> that we love the children of God: by loving God and carrying out his commands. <br> 3 In fact, this is love for God: to keep his commands. And his commands <br> are not burdensome, 4 for everyone born of God overcomes the world. <br> This is the victory that has overcome the world, even our faith. 5 Who <br> is it that overcomes the world? Only the one who believes that Jesus is the Son of God.</html>"); 

    userTypingRegion = new JTextField(); 

    userTypingRegion.addActionListener(this); 


    dataCollectionRegion.add(sampleWriting); 
    dataCollectionRegion.add(userTypingRegion); 

    ///Instructions South 

    JLabel instructions = new JLabel("Instruction: Type the " + 
           "passage as fast as possible in the space provided."); 
    instructions.setForeground(Color.white); 

    JLabel showResult = new JLabel("- - - Results - - -"); 

    showResult.setForeground(Color.white); 
    showResult.setHorizontalTextPosition(JLabel.CENTER); 

    results = new JTextArea(); 
    results.setEditable(false); 

    southRegion.add(instructions); 
    southRegion.add(Box.createRigidArea(new Dimension(0,5))); 
    southRegion.add(showResult); 
    southRegion.add(Box.createRigidArea(new Dimension(0,5))); 
    southRegion.add(results); 

    ///add cards 
     panels = new JPanel(new CardLayout()); 
     panels.add(infoPage); 
     panels.add(programFrame); 

     pane.add(switcher, BorderLayout.PAGE_START); 
     pane.add(panels, BorderLayout.CENTER); 

    } 

    public void actionPerformed(ActionEvent evt) 
    { 
     CardLayout layout = (CardLayout)(panels.getLayout()); 
     layout.next(panels); 
    } 

    class transferInfo implements ActionListener 
    { 
     public void actionPerformed(ActionEvent event) 
     { 
      results.setText("Selected: " + raceList.getSelectedItem());  
     } 
    } 

    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("Biometric Keystroke Dynamics"); 
     container TabChanger = new container(); 
     TabChanger.init(frame.getContentPane()); 
     frame.pack(); 
     frame.setResizable(false); 
     frame.setVisible(true); 
    } 
} 

您可以在代码结束时看到我解决此问题的尝试。我提前为我的杂乱编码道歉。 (这是我第一次)

+0

这很含糊。你能否添加更多的细节,而不是问括号内应该包含哪些内容?例如,“结果”的类型是什么? – 2012-02-24 05:41:16

+0

结果是JTextField类型,我希望用户选择。用户可用的选项是组合框中的字符串和int,或者复选框 – Maydayfluffy 2012-02-24 05:44:57

+0

您想要将用户选择从复选框或组合框打印到名为'results'的JTextField组件中?我们需要能够看到代码的其余部分,以便我能够帮助你。 – ninesided 2012-02-24 05:50:13

回答

0

所有你所提交的代码中的错误首先:

  1. 您声明JComboBox<Integer> ageEntries;JComboBox<String> raceList;为您的实例变量和为您localvariables你里面的init(...)方法。不要声明它们两次,只需要使用实例变量或局部变量,但不要使用它们两次。
  2. 当你创建一个JFrame对象时,总是让它成为你自己添加这个ilne frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);的习惯,这有助于以良好的方式关闭JFrame窗口。

现在写什么写在results.setText();。您可以在您的容器类中创建一个验证方法,该方法将返回boolean,并将在actionPerformed(...)方法NextButton内调用,并且在此方法内检查用户输入的合法与否的值,如果它们是合法值,则使用StringBuilder追加它好比说

StringBuilder sb = new StringBuilder(); // this will be your instance variable not local. 
sb.append((String) ageEntries.getSelectedItem()); 
sb.append(" "); // Add this space to distinguish between two values. 
// and so on for other values. 

一旦这样做,你可以写results.setText(containerObject.sb.toString());显示这些值。