问题
How would I produce the following window:
http://postimage.org/image/61aa8hrvb/
What would I use for formatting? Something similar to BorderLayout? Is there a better way?
I have tried using a combo of JFrame, JPanel and JTextArea; as follows:
public static void doListAllChecks() {
int transCount = CAObject.getTransCount();
JFrame frame = new JFrame();
frame.setVisible(true);
JPanel content = new JPanel();
for (int idx = 0; idx < transCount; idx++)
{
Transaction tObj = CAObject.getTrans(idx);
if (tObj != null) {
if (tObj.getTransId() == Constants.CHECK_ID)
{
System.out.println("Check ID " + tObj.getTransNumber() +
" Check Amount " + tObj.getTransAmount());
JTextArea textArea = new JTextArea(5,20);
textArea.setText("Check " + tObj.getTransAmount());
content.add(textArea, BorderLayout.EAST);
}
}
}
frame.setContentPane(content);
frame.setTitle("Dialog Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
I am looking to produce a basic and very simple style of a window. I have the data but I don't know how to produce the window.
Edit: I am not asking how to fill the window with data—just how to produce the window. It seems that it just has a fixed size (length and width) and a border. It seems like it is a barebones window.
Is there anything that you can think of the resembles this style of a window?
回答1:
Here is one proposal (it could be largely improved but at least, you'll have a startig point):
public static void main(String[] args) throws Exception {
String[][] transactions = new String[][] { { "0", "Check", "50.00" }, { "1", "svc.chrg.", "0.15" } };
JDialog f = new JDialog();
JTable table = new JTable(transactions, new String[] { "Id", "Type", "Amount" });
f.add(new JLabel("List all transactions:", JLabel.CENTER), BorderLayout.NORTH);
f.add(new JScrollPane(table));
f.setTitle("Dialog Display");
table.setPreferredSize(new Dimension(table.getPreferredSize().width, table.getRowHeight()
* transactions.length));
f.pack();
f.setSize(470, 120);
f.setLocationRelativeTo(null); // Center on screen
f.setVisible(true);
f.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
回答2:
Start with a JDialog (as you've asked)
Take a look at JTable to format the content
Take a look at BorderLayout
回答3:
Is there anything that you can think of the resembles this style of a window?
You might try setUndecorated(true), seen here, and maybe add a one-pixel border.
来源:https://stackoverflow.com/questions/11992506/java-dialog-box