Created
January 29, 2012 13:41
-
-
Save rhiguchi/1698877 to your computer and use it in GitHub Desktop.
Swing を MVC パターンで設計する流れの確認。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static javax.swing.SpringLayout.*; | |
import java.awt.BorderLayout; | |
import java.awt.Dimension; | |
import javax.swing.BorderFactory; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTable; | |
import javax.swing.JTextField; | |
import javax.swing.Spring; | |
import javax.swing.SpringLayout; | |
public class MainView { | |
public final JTable table = new JTable(0, 3); | |
public final JScrollPane tableScroll = new JScrollPane(table); | |
public final JLabel dateLabel = new JLabel("Date"); | |
public final JTextField dateField = new JTextField("2012-01-01"); | |
public final JLabel itemLabel = new JLabel("Item"); | |
public final JTextField itemField = new JTextField("Item"); | |
public final JLabel expenseLabel = new JLabel("Expense"); | |
public final JTextField expenseField = new JTextField("0"); | |
public final JPanel detailsPanel = | |
craeteDetailsPanel(dateLabel, dateField, itemLabel, itemField, expenseLabel, expenseField); | |
public final JPanel contentPane = createContentPane(detailsPanel, tableScroll); | |
public JPanel getContentPane() { | |
return contentPane; | |
} | |
JPanel craeteDetailsPanel( | |
JLabel dateLabel, JTextField dateField, JLabel itemLabel, JTextField itemField, | |
JLabel expenseLabel, JTextField expenseField) { | |
SpringLayout layout = new SpringLayout(); | |
JPanel detailsPanel = new JPanel(layout); | |
detailsPanel.add(dateLabel); | |
layout.putConstraint(WEST, dateLabel, 0, WEST, detailsPanel); | |
layout.putConstraint(NORTH, dateLabel, 0, NORTH, detailsPanel); | |
detailsPanel.add(dateField); | |
layout.putConstraint(WEST, dateField, 0, WEST, detailsPanel); | |
layout.putConstraint(NORTH, dateField, 2, SOUTH, dateLabel); | |
layout.getConstraints(dateField).setWidth(Spring.constant(120)); | |
detailsPanel.add(itemLabel); | |
layout.putConstraint(WEST, itemLabel, 8, EAST, dateField); | |
layout.putConstraint(BASELINE, itemLabel, 0, BASELINE, dateLabel); | |
detailsPanel.add(itemField); | |
layout.putConstraint(WEST, itemField, 8, EAST, dateField); | |
layout.putConstraint(BASELINE, itemField, 0, BASELINE, dateField); | |
detailsPanel.add(expenseLabel); | |
layout.putConstraint(WEST, expenseLabel, 8, EAST, itemField); | |
layout.putConstraint(BASELINE, expenseLabel, 0, BASELINE, dateLabel); | |
detailsPanel.add(expenseField); | |
layout.putConstraint(EAST, expenseField, 0, EAST, detailsPanel); | |
layout.putConstraint(BASELINE, expenseField, 0, BASELINE, dateField); | |
layout.putConstraint(EAST, itemField, -8, WEST, expenseField); | |
layout.getConstraints(expenseField).setWidth(Spring.constant(120)); | |
layout.getConstraints(detailsPanel).setHeight( | |
layout.getConstraint(SOUTH, expenseField)); | |
return detailsPanel; | |
} | |
JPanel createContentPane(JPanel detailsPanel, JScrollPane tablePane) { | |
JPanel contentPane = new JPanel(new BorderLayout()); | |
contentPane.setPreferredSize(new Dimension(640, 480)); | |
detailsPanel.setBorder(BorderFactory.createEmptyBorder(6, 12, 6, 12)); | |
contentPane.add(detailsPanel, "South"); | |
contentPane.add(tablePane, "Center"); | |
return contentPane; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.EventQueue; | |
import javax.swing.JFrame; | |
public class MainViewTest { | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
JFrame frame = new JFrame("MainViewTest"); | |
MainView mainView = new MainView(); | |
frame.setContentPane(mainView.getContentPane()); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.EventQueue; | |
import javax.swing.JFrame; | |
public class SwingMVCTest { | |
public void launch() { | |
JFrame frame = createMainFrame(); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
} | |
JFrame createMainFrame() { | |
JFrame frame = new JFrame("Swing MVC Test"); | |
MainView mainView = new MainView(); | |
frame.setContentPane(mainView.getContentPane()); | |
return frame; | |
} | |
public static void main(String[] args) { | |
final SwingMVCTest test = new SwingMVCTest(); | |
EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
test.launch(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment