2013-12-12 58 views
1

我想創建aj框架,用戶在J中放置一個問題值文本字段和應用程序顯示數學符號,但我不知道它是否存在Java庫中的任何包或任何其他技術像二次問題在java中使用jlabel顯示數學公式

a=5 
b=10 
c=15 

顯示

ax^2+10x=15 

D=b^2-4ac 

alpha=(-b+root(D)/2a 


beta=(-b-root(D)/2a 

說得非常難看,我想顯示喜歡數學符號,請給我任何的想法,教程等.... 我的代碼

package linerEquations; 

    import javax.swing.*; 

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

    class MatrixCalculator extends JFrame implements ActionListener{ 

     JLabel jlTitle,Xa1,Yb1,C1; 
     JLabel Xa2,Yb2,C2; 
     JPanel jp; 
     JTextField jtXa1,jtYb1,jtC1; 
     JTextField jtXa2,jtYb2,jtC2; 
     JButton jbExit,jbClear,jbSolution,jbShowGraph; 
     static MatrixCalculator fram; 

     MatrixCalculator(){ 
      setTitle("Matrix Calculator"); 
      setVisible(true); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setLocationRelativeTo(null); 
      getContentPane().setLayout(null); 
      setSize(500,265); 
      setResizable(false); 
      //jPanle 
      jp=new JPanel(); 
      jp.setLocation(10, 11); 
      jp.setLayout(null); 
      jp.setSize(464,194); 
      getContentPane().add(jp); 
      //Title 
      jlTitle =new JLabel("Linear Equestion of Two Variable"); 
      jlTitle.setFont(new Font("Tahoma", Font.BOLD, 19)); 
      jlTitle.setBounds(61,11,332,20); 
      jp.add(jlTitle); 
      //Label 
      //Xa1 
      Xa1 =new JLabel("Xa1= "); 
      Xa1.setBounds(78,64,50,20); 
      jp.add(Xa1); 
      //Yb1 
      Yb1 =new JLabel("Yb1= "); 
      Yb1.setBounds(178,64,50,20); 
      jp.add(Yb1); 
      //C1 
      C1 =new JLabel("C1= "); 
      C1.setBounds(278,64,50,20); 
      jp.add(C1); 
      //JTextField 
      //jtXa1 
      jtXa1 =new JTextField(); 
      jtXa1.setBounds(108,64,50,20); 
      jp.add(jtXa1); 
      //jtYb1 
      jtYb1 =new JTextField(); 
      jtYb1.setBounds(208,64,50,20); 
      jp.add(jtYb1); 
      //jtC1 
      jtC1 =new JTextField(); 
      jtC1.setBounds(308,64,50,20); 
      jp.add(jtC1); 
      //Label 2 
      //Xa2 
      Xa2 =new JLabel("Xa2= "); 
      Xa2.setBounds(78,94,50,20); 
      jp.add(Xa2); 
      //Yb2 
      Yb2 =new JLabel("Yb2= "); 
      Yb2.setBounds(178,94,50,20); 
      jp.add(Yb2); 
      //C2 
      C2 =new JLabel("C2= "); 
      C2.setBounds(278,94,50,20); 
      jp.add(C2); 
      //JTextField 
      //jtXa2 
      jtXa2 =new JTextField(); 
      jtXa2.setBounds(108,94,50,20); 
      jp.add(jtXa2); 
      //jtYb2 
      jtYb2 =new JTextField(); 
      jtYb2.setBounds(208,94,50,20); 
      jp.add(jtYb2); 
      //jtC2 
      jtC2 =new JTextField(); 
      jtC2.setBounds(308,94,50,20); 
      jp.add(jtC2); 

      //Button 
      //Solution Button 
      jbSolution =new JButton("Solution"); 
      jbSolution.setBounds(20,150,80,30); 
      jbSolution.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        new Solution(fram); 

       } 
      }); 
      jp.add(jbSolution); 
      //ShowGraph Button 
      jbShowGraph =new JButton("Show Graph"); 
      jbShowGraph.setBounds(108,150,117,30); 
      jp.add(jbShowGraph); 
      //Clear Button 
      jbClear =new JButton("Clear"); 
      jbClear.setBounds(249,150,80,30); 
      jp.add(jbClear); 
      //Exit Button 
      jbExit =new JButton("Exit"); 
      jbExit.setBounds(352,150,80,30); 
      jbExit.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        System.exit(0); 

       } 
      }); 
      jp.add(jbExit); 

     } 

     public static void main(String args[]){ 
      fram=new MatrixCalculator(); 
     } 

     public void actionPerformed(ActionEvent e){ 

     } 


    } 
+2

一個問題通常在最後有一個'?'...... – bobbel

+0

什麼東西在你的代碼中不起作用? – alex2410

+0

你的意思是[this](http://stackoverflow.com/questions/456002/displaying-fancy-equations-with-java) – MadProgrammer

回答

3

您不需要任何庫,您可以將字符串格式化爲HTML並顯示它。

例如,如果你想在jbSolution按鈕顯示ax^2+10x=15,你應該這樣做:

int a=5, b=10, c=15; 

    String j = "<html>"+a+"x<sup>2</sup>+"+b+" x = "+c+"</html>"; 
    jbSolution =new JButton(j); 
    jbSolution.setBounds(20,150,130,30); 

爲d = B^2-4ac

String j = "<html>D = b<sup>2</sup> - 4ac</html>"; 

對於β=( - b-根(D)/ 2a

String j = "<html>beta=(-b-\u221AD)/2a</html>"; 
+0

而你會如何表示平方根? –

+0

@Vash用字符串更新了OP中提到的所有方程的答案 –