2015-01-25 2 views
1

내 JText 영역이 작동하지 않습니다. 이 코드를 실행하면 볼 수 있습니다. textArea는 필자가해야 할 명확한 지점을 설정 했음에도 불구하고 다른 장소로 계속 이동합니다. '고통'텍스트 영역자바 텍스트 영역 오류

를 포함하는 JPanel의이다 : 텍스트 영역의 코드는 내가 정확히 testArea 참고가 57

라인에 설명되어 고정 된 위치를 유지하기위한 것입니다 무엇이 필요 라인 57

입니다

public Graphics g; 

public void paint(Graphics g) { 

    Graphics2D projectFinder = (Graphics2D) g; 
    projectFinder.setColor(Color.GRAY); 
    projectFinder.fillRect(0, 0, 1000, 50); 
    projectFinder.setStroke(new BasicStroke(100)); 

    Graphics2D OutPut = (Graphics2D) g; 
    OutPut.setColor(Color.LIGHT_GRAY); 
    OutPut.fillRect(553, 60, 535, 670); 
    OutPut.drawString("Project Input (Your Code)", 30, 90); 

    Graphics2D console = (Graphics2D) g; 
    console.setColor(Color.WHITE); 
    console.fillRect(563, 620, 515, 100); 

    console.setColor(Color.BLACK); 
    console.drawString("Console.ChemLOG", 570, 640); 

} 

public static void main(String[] args) { 

    JPanel pnlButton = new JPanel(); 
    pnlButton.setBounds(800, 100, 100, 100); 

    JButton button = new JButton("Add Project"); 
    button.setBounds(1000, 0, 100, 50); 

    JFrame frame = new JFrame("GenoTECH IDE 1.0.0"); 
    frame.add(new MainScreen()); 
    frame.setSize(1100, 800); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(button); 
    button.addActionListener(new NewProject()); 

    // TextBox 

    JPanel pain = new JPanel(); 
    JTextArea area = new JTextArea(); 
    area.setSize(535, 670); 
    area.setLocation(0, 0); 

    pain.setLocation(10, 60); 
    pain.setSize(area.getSize()); 

    pain.add(area); 

    frame.add(pain); 
} 

// add project button 
static class NewProject implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 

     JButton buttonOK = new JButton("Ok"); 
     buttonOK.setBounds(0, 233, 150, 45); 

     JButton buttonCn = new JButton("Cancel"); 
     buttonCn.setBounds(150, 233, 150, 45); 

     JFrame frame2 = new JFrame("New Project"); 
     frame2.setSize(300, 300); 
     frame2.setVisible(true); 
     frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame2.add(buttonOK); 
     frame2.add(buttonCn); 
     buttonCn.addActionListener(new buttonCN()); 

    } 

    static class buttonCN implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      System.exit(0); 

     } 
    } 
} 

} 코드에 대한 업데이 트에 대한 지금 확인

, 나는 JTextArea에가 그러나 2D 그래픽은 어떤 이유로이라고 점점되지 않으며, 엉망으로하지 않고 일을 가지고있다. 일부 Java 레이아웃 형식을 사용하는 다른 레이아웃 작업도하고 있습니다.

새로운 코드 :

private static final long serialVersionUID = 1L; 
public Graphics g; 

public void paint(Graphics g) { 

    Graphics2D projectFinder = (Graphics2D) g; 
    projectFinder.setColor(Color.GRAY); 
    projectFinder.fillRect(0, 0, 1000, 50); 
    projectFinder.setStroke(new BasicStroke(100)); 

    Graphics2D OutPut = (Graphics2D) g; 
    OutPut.setColor(Color.LIGHT_GRAY); 
    OutPut.fillRect(553, 60, 535, 670); 
    OutPut.drawString("Project Input (Your Code)", 30, 90); 

    Graphics2D console = (Graphics2D) g; 
    console.setColor(Color.WHITE); 
    console.fillRect(563, 620, 515, 100); 

    console.setColor(Color.BLACK); 
    console.drawString("Console.ChemLOG", 570, 640); 

} 

public static void main(String[] args) {   
    JButton button = new JButton("Add Project"); 
    button.setBounds(1000, 0, 100, 50); 
    //button.setSize(5,5); 
    //button.setLocation(1,1); 

    JFrame frame = new JFrame("GenoTECH IDE 1.0.0"); 
    frame.setLayout(null); 
    frame.add(new MainScreen()); 
    frame.setSize(1100, 800); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(button); 
    button.addActionListener(new NewProject()); 

    // Thing to Change 
    JTextArea area = new JTextArea(); 
    area.setSize(535, 670); 
    area.setLocation(10, 60); 

    frame.add(area); 
} 


// add project button 
static class NewProject implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 

     JButton buttonOK = new JButton("Ok"); 
     buttonOK.setBounds(0, 233, 150, 45); 

     JButton buttonCn = new JButton("Cancel"); 
     buttonCn.setBounds(150, 233, 150, 45); 

     JFrame frame2 = new JFrame("New Project"); 
     frame2.setSize(300, 300); 
     frame2.setVisible(true); 
     frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame2.add(buttonOK); 
     frame2.add(buttonCn); 
    } 
} 
+0

프로그램을 실행할 때마다 다른 위치에 표시됩니까? (예 : 프로그램 실행과 동시에 발생합니까?) 또는이 동작이 발생해야하는 것이 있습니까? – user1849060

+0

무언가를 입력 할 때마다 JPanel은 크기 나 위치 또는 둘 다를 변경합니다. –

+4

경계 및 치수를 하드 코딩하는 대신 레이아웃 관리자를 사용하십시오. http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html –

답변

2

나는 당신이 뭘하려는 건지 모르겠어요. Swing 레이아웃을 가진 코드의 실행 가능한 버전이 있습니다.

변경 사항은 다음과 같습니다.

  1. SwingUtilities invokeLater 메서드를 호출하여 응용 프로그램을 시작했습니다. 이렇게하면 Event Dispatch thread (EDT)에서 Swing 구성 요소가 만들어지고 수정됩니다.

  2. JFrame 메서드를 실행하는 순서는 매우 중요합니다. 나는 명령을 고쳤다.

  3. paintComponent 메소드를 사용하여 JPanel에 그릴 수 있습니다. 드로잉 코드 전에 super를 호출해야합니다.

  4. 그리기 패널의 크기 만 설정합니다. JFrame 팩 메서드를 사용하여 나머지 Swing 구성 요소의 크기를 조정합니다.

  5. 다른 언급했듯이 Swing layouts을 사용하여 스윙 구성 요소를 배치합니다.

.

package com.ggl.testing; 

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 

public class GenoTech implements Runnable { 

    @Override 
    public void run() { 
     JFrame frame = new JFrame("GenoTECH IDE 1.0.0"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel mainPanel = new JPanel(); 
     mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS)); 

     mainPanel.add(new DrawingPanel()); 

     JPanel pnlButton = new JPanel(); 

     JButton button = new JButton("Add Project"); 
     button.addActionListener(new NewProjectActionListener()); 
     pnlButton.add(button); 

     mainPanel.add(pnlButton); 

     JPanel pain = new JPanel(); 
     JTextArea area = new JTextArea(); 
     pain.add(area); 

     mainPanel.add(pain); 

     frame.add(mainPanel); 

     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new GenoTech()); 
    } 

    public class DrawingPanel extends JPanel { 

     private static final long serialVersionUID = -5718559965267054844L; 

     public DrawingPanel() { 
      this.setPreferredSize(new Dimension(700, 300)); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      Graphics2D g2d = (Graphics2D) g; 
      g2d.setColor(Color.GRAY); 
      g2d.fillRect(0, 0, 700, 50); 
      g2d.setStroke(new BasicStroke(100)); 

      g2d.setColor(Color.LIGHT_GRAY); 
      g2d.fillRect(553, 60, 535, 300); 
      g2d.drawString("Project Input (Your Code)", 30, 90); 

      g2d.setColor(Color.WHITE); 
      g2d.fillRect(563, 620, 515, 100); 

      g2d.setColor(Color.BLACK); 
      g2d.drawString("Console.ChemLOG", 570, 640); 
     } 

    } 

    // add project button 
    public class NewProjectActionListener implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame2 = new JFrame("New Project"); 
      frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

      JPanel mainPanel = new JPanel(); 

      JButton buttonOK = new JButton("Ok"); 
      mainPanel.add(buttonOK); 

      JButton buttonCn = new JButton("Cancel"); 
      buttonCn.addActionListener(new ButtonCNActionListener()); 
      mainPanel.add(buttonCn); 

      frame2.add(mainPanel); 
      frame2.pack(); 
      frame2.setLocationByPlatform(true); 
      frame2.setVisible(true); 
     } 

     public class ButtonCNActionListener implements ActionListener { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.exit(0); 
      } 
     } 
    } 

} 
+0

좋아, 네가 한 일을 이해하지만, 나는 조용하게 문제를 해결했다고 생각하지 않는다. 나는 당신의 코드를 사용하여 나의 일부분을 정리했다. 그러나 나의 주요한 문제는 JTextArea가 타이핑 할 때, 화면이 엉망이되어 화면의 맨 위로 이동한다는 것이다. 코드가이를 정확히 수정하지는 않았지만 권장 사항에 대해 감사드립니다. 이 문제를 해결하기 위해 레이아웃을 사용하려고 노력 중입니다. –

+0

@ RadékMartinez :이 답변이 어떤 식 으로든 도움이된다면 적어도 투표를하시기 바랍니다. 질문에 대답하면 받아 들여야합니다. 이 사이트에 대한 최상의 행동에 대해 자세히 알아 보려면 [내 질문에 대한 답변을하면 어떻게합니까?] (http://stackoverflow.com/help/someone-answers)를 확인하십시오. –