如何使用我的代码打印颜色

时间:2014-11-02 13:16:15

标签: java

我有这个代码从文件中读取并使用模式打印出来。

打印示例:

水果 appeles = 10 bannanas = 5

我如何使用颜色进行打印,因为我想用红色打造苹果,用黄色打造bannanas字样

如何使用颜色打印? 我的意思是" TipMaterialKamera2"使用红色打印,itemMatcherKamera2是否为黄色?

/////////////////////////////////////////////// //////////////////// BUTON COUNT KAMERA Garnitura + buton + rolare ////////////////////// /////////////////////////////////////////////

 JTextPane Kamera22 = new JTextPane();
 Kamera22.setForeground(Color.blue);

 Kamera22.setEditable(false);
 Kamera22.setBounds(330, 63, 338, 257);
 framePrincipal.getContentPane().add(Kamera22);

         JButton btnKamera2 = new JButton("Count Kamera 2");
         btnKamera2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String lineKamera2 = null;



                Pattern CategorieKamera2 = Pattern.compile ("\\{([^}]*)\\}");
                Pattern CategorieSiCantitateKamera2 = Pattern.compile("^(\\w+)=(\\d+)$"); // CITESTE TOT CE ESTE SUB CATEGORIA ( ) SI AFISAZA VALORILE CUVANT=VALOARE EX: Nio=10\Io=15



                StringBuilder resultKamera2 = new StringBuilder();

                {
                try (BufferedReader brKamera2 = new BufferedReader(new FileReader("D:/test1.txt"))) {
                while ((lineKamera2 = brKamera2.readLine()) != null) {

                Matcher categoryMatcherKamera2 = CategorieKamera2.matcher(lineKamera2);                    
                Matcher itemMatcherKamera2 = CategorieSiCantitateKamera2.matcher(lineKamera2);


                if (categoryMatcherKamera2.matches()) {
                if (resultKamera2.length() > 0) { // found new category, put on new line

                    resultKamera2.append(System.getProperty("line.separator"));

                }


                String Kamera2 = categoryMatcherKamera2.group(1); // Citeste tot ce apare cu Nio \ IO Pentru Sockel ( CITESTE TOT CE ESTE SUB CATEGORIA (SOCKEL)!)




                //resultKamera2.append(String.format("<font color=\"%s\">%s</font>",(Kamera2).append(" ")));


                //result.append(System.getProperty("line.separator"));
                } else if (itemMatcherKamera2.matches()) {

                String TipMaterialKamera2 = itemMatcherKamera2.group(1); // NIo\IO
                String CantitateKamera2 = itemMatcherKamera2.group(2); // 6 bucati , 8 , 10 ... n bucati
                resultKamera2.append(System.getProperty("line.separator"));
                resultKamera2.append(TipMaterialKamera2).append(": ")// Nio\IO:
                .append(CantitateKamera2) // Nio\Io 6
                .append(" bucati "); // Afiseaza cuv. "bucati" dupa cantitate


                }
                Kamera22.setText(resultKamera2.toString());
                }
                } catch (FileNotFoundException e1) {

                e1.printStackTrace();
                } catch (IOException e1) {

                e1.printStackTrace();
                    }
                 }

            }

         });

1 个答案:

答案 0 :(得分:0)

如果您需要使用不同颜色的按钮上的文字,请尝试:

if (myString.equals("TipMaterialKamera2")) {//use switch if you are on jdk 7
    btnKarema2.setForeground(java.awt.Color.RED);
} else if (myString.equals("itemMatcherKamera2")){
    btnKarema2.setForeground(java.awt.Color.YELLOW);
} else {
    btnKarema2.setForeground(java.awt.Color.GREEN);/default
}