如果单击另一个JComboBox,JComboBox文本将更改

时间:2014-09-18 16:07:38

标签: java swing jcombobox

我是java的新手。我的问题是,我的代码中有两个JComboBox。每次我尝试单击组合框时,其中的文本将更改为其默认文本。但是当我将鼠标悬停时,我会点击我点击的文字。我该怎么办?这是我的代码。谢谢你的帮助!

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.print.*;

public class printAndPreview implements Printable, ActionListener 
{
     //integer sizes
    Integer[] sizes = { 7, 8, 9, 10, 11, 12, 14, 18, 20, 22, 24, 36 };

    //to get font
    GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String []fonts = g.getAvailableFontFamilyNames();

    JFrame frameToPrint;
    @SuppressWarnings("rawtypes")
    JComboBox forFont =new JComboBox(fonts); ;
    @SuppressWarnings({ "rawtypes", "unchecked" })
    JComboBox forSize = new JComboBox(sizes) ;
    JCheckBox boldCheck, italCheck;



    Main foo;

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

        //if there is no page
        if (page > 0)
        { 
            return NO_SUCH_PAGE;
        }

         //User (0,0) is typically outside the imageable area, so we must
         //translate by the X and Y values in the PageFormat to avoid clipping

        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        //Now print the window and its visible contents
        frameToPrint.printAll(g);

        //tell the caller that this page is part of the printed document
        return PAGE_EXISTS;
    }

    public void actionPerformed(ActionEvent e) 
    {
         //implementation of printing
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         //the print dialog
         boolean ok = job.printDialog();

         if (ok)
         {
             try
             {
                  job.print();
             }
             catch (PrinterException ex) 
             {
              /* The job did not successfully complete */
                 System.out.println("Error in printing");
             }
         }
    }
    //UI printing
    public printAndPreview(JFrame f) 
    {
        frameToPrint = f;
    }



    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void userPreview(String text)
    {

          JFrame f = new JFrame("Print and Preview");

          f.addWindowListener(new WindowAdapter() 
          {

            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
          });


          //print button
          FontListener fl = new FontListener();
          System.out.println("Guba?");

          JScrollPane pane = new JScrollPane();
          JButton printButton = new JButton("Print");
         // forFont = new JComboBox(fonts);
         // forSize = new JComboBox(sizes);
          JLabel size = new JLabel("Size");
          JLabel font =new JLabel("Font");

          pane.setPreferredSize(new Dimension(250,400));
          printButton.addActionListener(new printAndPreview(f));


          italCheck = new JCheckBox("Ital");
          boldCheck = new JCheckBox("Bold");

          //Action Listener
          forFont.addActionListener(fl);
          System.out.println("Guba ne?");

          forSize.addActionListener(fl);
          italCheck.addActionListener(fl);
          boldCheck.addActionListener(fl);

          //setBounds for comboBox and all
          font.setBounds(10, 420, 50, 30);  //JLabel
          forFont.setBounds(60, 420, 100, 30); //comboBox

          size.setBounds(200, 420, 50, 30); //JLabel
          forSize.setBounds(250, 420, 50, 30); //comboBox

          boldCheck.setBounds(350, 420, 50,30); //JCheckBox
          italCheck.setBounds(400,420,50,30); //JCheckBox

          //setBounds(x, y, width, height);


          //this code block is for the JFrame
          //f.setPreferredSize(new Dimension(420, 520));

          f.add("South",printButton);
          f.add(forFont);
          f.add(forSize);
          f.add(font);
          f.add(size);
          f.add(boldCheck);
          f.add(italCheck);

          foo = new Main(text);

          f.getContentPane().add(foo);
          f.setPreferredSize(new Dimension(640,520)); //this should be dynamic
          f.setVisible(true);
          System.out.println("Guba?");
          f.pack(); //this will guba the code

    }   


    private class FontListener implements ActionListener 
    {

     public void actionPerformed(ActionEvent e) 
     {
        updateText();
     }

      public void updateText() 
      {
          String name = (String) forFont.getSelectedItem();
          Integer size = (Integer) forSize.getSelectedItem();


        int style;
        if (boldCheck.isSelected() && italCheck.isSelected())
          style = Font.BOLD | Font.ITALIC;
        else if (boldCheck.isSelected())
          style = Font.BOLD;
        else if (italCheck.isSelected())
          style = Font.ITALIC;
        else
          style = Font.PLAIN;

        Font f = new Font(name, style, size.intValue());
        foo.setFont(f);
        foo.repaint();
      }
    }


}

0 个答案:

没有答案