gpt4 book ai didi

java - 使用 Java Swing 在选项卡式窗口中显示输出

转载 作者:行者123 更新时间:2023-12-02 07:47:29 25 4
gpt4 key购买 nike

我用java开发了一个Linux任务管理器。目前的输出显示在控制台中,而选项卡式窗口则单独显示(这是使用 java swing 完成的)。现在我希望控制台的输出显示在选项卡式窗口中。我该怎么做?我使用过一些类(class)。一个用于任务管理器功能,另一个用于 GUI。我已将代码粘贴在下面,以便有人帮助我。

**TabbedPaneDemo1.java**

package components;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;


/*
* TabbedPaneDemo.java requires one additional file:
* images/middle.gif.
*/

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class TabbedPaneDemo1
{

public static void main(String args[]) throws Exception
{
try
{
TextArea textarea = new TextArea();
TabbedPaneDemo obj = new TabbedPaneDemo();
obj.fn();
String line;
String result = "";
FileOutputStream out;
//FileOutputStream out; // declare a file output object
PrintStream p;
Process p1 = Runtime.getRuntime().exec("tasklist.exe");
out = new FileOutputStream("myfile.txt");//write to a file//
p = new PrintStream( out );
BufferedReader input = new BufferedReader(new InputStreamReader(p1.getInputStream()));//read form a file//
while ((line = input.readLine()) != null)
{
System.out.println(line);
//textarea.append(line + "\n");
result += line+"\n";
p.println (line);
//textarea.setVisible(true);
}
//msgBox(result);
p.close();
input.close();
}
catch(Exception e)
{

}
}
public static void msgBox(String msg) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, msg, "WindowsUtils",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}

**TabbedPaneDemo.java**

package components;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

public class TabbedPaneDemo extends JPanel {
Integer i;
public TabbedPaneDemo() {
super(new GridLayout(1, 1));

JTabbedPane tabbedPane = new JTabbedPane();
ImageIcon icon = createImageIcon("images");


JComponent panel1 = makeTextPanel("tasklist");
tabbedPane.addTab("tasks", icon, panel1,
"ta");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

JComponent panel2 = makeTextPanel("windows");
tabbedPane.addTab("wins", icon, panel2,
"wi");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);


add(tabbedPane);`enter code here`
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeTextPanel(String text)
{
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = TabbedPaneDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TabbedPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void fn() {
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}

最佳答案

如果您使用 swing 创建了 JTabbedPane ,用内容更新它的最简单方法可能是向其中添加一个 JTextArea,如下所示:

tab.add(textarea);

然后用您希望显示的内容更新 textarea

textarea.setText(); // or append();

要通过流添加来自控制台的输入,请执行以下操作:

BufferedReader input = new BufferedReader(
new InputStreamReader(consoleProcess.getInputStream()));

String text;

while ((text = input.readLine()) != null)
{
textarea.append(text + "\n");
}
input.close();

关于java - 使用 Java Swing 在选项卡式窗口中显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10634265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com