因此,我使用 Oracle 選項卡式示例創建了一個 Java swing 應用程式,以幫助從一家非常小的公司的內部資料庫中快速檢索資料,而且我對 Java 還是很陌生(不過,腳本撰寫得很好)。我的問題是從資料庫中檢索到的資料超出了選項卡邊界
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
// Replace this with your text retrieval process
List<String> text = new ArrayList<String>(128);
try (BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/StarWarANewHope.txt")))) {
String line = null;
while ((line = br.readLine()) != null) {
text.add(line);
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
// Use this to format the results for the text area
StringJoiner joiner = new StringJoiner("\n");
for (String line : text) {
joiner.add(line);
}
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Customers", null, new TestPane(joiner.toString()), "Displays customer details");
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tabbedPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane(String text) {
JTextArea filler = new JTextArea(text, 25, 25);
filler.setWrapStyleWord(true);
filler.setLineWrap(true);
JScrollPane scroll = new JScrollPane(filler);
//filler.setHorizontalAlignment(JTextArea.CENTER);
setLayout(new BorderLayout());
//panel.add(filler);
add(scroll);
}
}
}
如果您仍然有問題,請考慮提供一個最小的可重現示例,它消除了猜測作業,通常會為更多細節提供更好的答案
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429879.html
