java 个文本区域出错
Error with java text areas
我的 JText 区域不工作。你会看到你是否 运行 这个代码。 textArea 一直去不同的地方,即使我已经设置了一个明确的位置,它应该是。 textArea 的代码是第 57 行。
我真正需要的是让 testArea 保持固定位置,这在第 57 行中有解释。
注意:'pain' 是一个包含 textArea
的 JPanel
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);
}
}
我不确定你想做什么。这是带有 Swing 布局的代码的可运行版本。
这是我所做的更改。
我通过调用 SwingUtilities invokeLater 方法启动了应用程序。这确保了在 Event Dispatch thread (EDT).
上创建和修改 Swing 组件
执行 JFrame 方法的顺序非常重要。我确定了订单。
您使用 paintComponent 方法在 JPanel 上绘图。您必须在绘图代码之前调用 super。
您只设置绘图面板的大小。您使用 JFrame pack 方法来调整其余 Swing 组件的大小。
正如其他人提到的,您使用 Swing layouts 来布置 Swing 组件。
.
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);
}
}
}
}
我的 JText 区域不工作。你会看到你是否 运行 这个代码。 textArea 一直去不同的地方,即使我已经设置了一个明确的位置,它应该是。 textArea 的代码是第 57 行。
我真正需要的是让 testArea 保持固定位置,这在第 57 行中有解释。
注意:'pain' 是一个包含 textArea
的 JPanelpublic 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);
}
}
我不确定你想做什么。这是带有 Swing 布局的代码的可运行版本。
这是我所做的更改。
我通过调用 SwingUtilities invokeLater 方法启动了应用程序。这确保了在 Event Dispatch thread (EDT).
上创建和修改 Swing 组件
执行 JFrame 方法的顺序非常重要。我确定了订单。
您使用 paintComponent 方法在 JPanel 上绘图。您必须在绘图代码之前调用 super。
您只设置绘图面板的大小。您使用 JFrame pack 方法来调整其余 Swing 组件的大小。
正如其他人提到的,您使用 Swing layouts 来布置 Swing 组件。
.
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);
}
}
}
}