
Эксперт
  
Профиль
Группа: Завсегдатай
Сообщений: 1801
Регистрация: 25.4.2006
Репутация: 16 Всего: 40
|
Вот был пример из туториала Qt Jambi Код | import com.trolltech.qt.core.Qt; import com.trolltech.qt.gui.*;
public class LineEdits extends QWidget {
private QLineEdit echoLineEdit; private QLineEdit validatorLineEdit; private QLineEdit alignmentLineEdit; private QLineEdit inputMaskLineEdit; private QLineEdit accessLineEdit;
public LineEdits() { this(null); }
public LineEdits(QWidget parent) { super(parent); QGroupBox echoGroup = new QGroupBox(tr("Echo"));
QLabel echoLabel = new QLabel(tr("Mode:")); QComboBox echoComboBox = new QComboBox(); echoComboBox.addItem(tr("Normal")); echoComboBox.addItem(tr("Password")); echoComboBox.addItem(tr("No Echo"));
setWindowIcon(new QIcon("classpath:com/trolltech/images/qt-logo.png"));
echoLineEdit = new QLineEdit(); echoLineEdit.setFocus(); //////////////////
QGroupBox validatorGroup = new QGroupBox(tr("Validator"));
QLabel validatorLabel = new QLabel(tr("Type:")); QComboBox validatorComboBox = new QComboBox(); validatorComboBox.addItem(tr("No validator")); validatorComboBox.addItem(tr("Integer validator")); validatorComboBox.addItem(tr("Double validator"));
validatorLineEdit = new QLineEdit(); ////////////////////
QGroupBox alignmentGroup = new QGroupBox(tr("Alignment"));
QLabel alignmentLabel = new QLabel(tr("Type:")); QComboBox alignmentComboBox = new QComboBox(); alignmentComboBox.addItem(tr("Left")); alignmentComboBox.addItem(tr("Centered")); alignmentComboBox.addItem(tr("Right"));
alignmentLineEdit = new QLineEdit();
///////////////////////////////////////
QGroupBox inputMaskGroup = new QGroupBox(tr("Input mask"));
QLabel inputMaskLabel = new QLabel(tr("Type:")); QComboBox inputMaskComboBox = new QComboBox(); inputMaskComboBox.addItem(tr("No mask")); inputMaskComboBox.addItem(tr("Phone number")); inputMaskComboBox.addItem(tr("ISO date")); inputMaskComboBox.addItem(tr("License key"));
inputMaskLineEdit = new QLineEdit();
///////////////////////////////////////
QGroupBox accessGroup = new QGroupBox(tr("Access"));
QLabel accessLabel = new QLabel(tr("Read-only:")); QComboBox accessComboBox = new QComboBox(); accessComboBox.addItem(tr("False")); accessComboBox.addItem(tr("True"));
accessLineEdit = new QLineEdit();
////////////////////////////////////////////
echoComboBox.activatedIndex.connect(this, "echoChanged(int)"); validatorComboBox.activatedIndex.connect(this, "validatorChanged(int)"); alignmentComboBox.activatedIndex.connect(this, "alignmentChanged(int)"); inputMaskComboBox.activatedIndex.connect(this, "inputMaskChanged(int)"); accessComboBox.activatedIndex.connect(this, "accessChanged(int)");
/////////////////////////////////////////////
QGridLayout echoLayout = new QGridLayout(); echoLayout.addWidget(echoLabel, 0, 0); echoLayout.addWidget(echoComboBox, 0, 1); echoLayout.addWidget(echoLineEdit, 1, 0, 1, 2); echoGroup.setLayout(echoLayout);
//////////////////////////////////////////
QGridLayout validatorLayout = new QGridLayout(); validatorLayout.addWidget(validatorLabel, 0, 0); validatorLayout.addWidget(validatorComboBox, 0, 1); validatorLayout.addWidget(validatorLineEdit, 1, 0, 1, 2); validatorGroup.setLayout(validatorLayout);
QGridLayout alignmentLayout = new QGridLayout(); alignmentLayout.addWidget(alignmentLabel, 0, 0); alignmentLayout.addWidget(alignmentComboBox, 0, 1); alignmentLayout.addWidget(alignmentLineEdit, 1, 0, 1, 2); alignmentGroup.setLayout(alignmentLayout);
QGridLayout inputMaskLayout = new QGridLayout(); inputMaskLayout.addWidget(inputMaskLabel, 0, 0); inputMaskLayout.addWidget(inputMaskComboBox, 0, 1); inputMaskLayout.addWidget(inputMaskLineEdit, 1, 0, 1, 2); inputMaskGroup.setLayout(inputMaskLayout);
QGridLayout accessLayout = new QGridLayout(); accessLayout.addWidget(accessLabel, 0, 0); accessLayout.addWidget(accessComboBox, 0, 1); accessLayout.addWidget(accessLineEdit, 1, 0, 1, 2); accessGroup.setLayout(accessLayout);
//////////////////////////////////////////////
QGridLayout layout = new QGridLayout(); layout.addWidget(echoGroup, 0, 0); layout.addWidget(validatorGroup, 1, 0); layout.addWidget(alignmentGroup, 2, 0); layout.addWidget(inputMaskGroup, 0, 1); layout.addWidget(accessGroup, 1, 1); setLayout(layout);
setWindowTitle(tr("Line Edits")); }
public void echoChanged(int index) { switch (index) { case 0: echoLineEdit.setEchoMode(QLineEdit.EchoMode.Normal); break; case 1: echoLineEdit.setEchoMode(QLineEdit.EchoMode.Password); break; case 2: echoLineEdit.setEchoMode(QLineEdit.EchoMode.NoEcho); } }
public void validatorChanged(int index) { switch (index) { case 0: validatorLineEdit.setValidator(null); break; case 1: validatorLineEdit.setValidator(new QIntValidator(validatorLineEdit)); break; case 2: validatorLineEdit.setValidator(new QDoubleValidator(-999.0, 999.0, 2, validatorLineEdit)); }
validatorLineEdit.setText(""); }
public void alignmentChanged(int index) { switch (index) { case 0: alignmentLineEdit.setAlignment(new Qt.Alignment(Qt.AlignmentFlag.AlignLeft)); break; case 1: alignmentLineEdit.setAlignment(new Qt.Alignment(Qt.AlignmentFlag.AlignCenter)); break; case 2: alignmentLineEdit.setAlignment(new Qt.Alignment(Qt.AlignmentFlag.AlignRight)); } }
public void inputMaskChanged(int index) { switch (index) { case 0: inputMaskLineEdit.setInputMask(""); break; case 1: inputMaskLineEdit.setInputMask("+99 99 99 99 99;_"); break; case 2: inputMaskLineEdit.setInputMask("0000-00-00"); inputMaskLineEdit.setText("00000000"); inputMaskLineEdit.setCursorPosition(0); break; case 3: inputMaskLineEdit.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#"); } }
public void accessChanged(int index) { switch (index) { case 0: accessLineEdit.setReadOnly(false); break; case 1: accessLineEdit.setReadOnly(true); } }
public static void main(String args[]) { QApplication.initialize(args);
LineEdits lineedits = new LineEdits(); lineedits.show();
QApplication.exec(); } }
|
Вот недоделанный аналог на SWING Код | import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class JavaLineEdits extends JFrame { private JTextField echoLineEdit; private JTextField validatorLineEdit; private JTextField alignmentLineEdit; private JTextField inputMaskLineEdit; private JTextField accessLineEdit;
public JavaLineEdits() { JPanel echoGroup = new JPanel(new GridBagLayout()); echoGroup.setBorder(BorderFactory.createTitledBorder("Echo")); echoGroup.setLayout(new BoxLayout(echoGroup, BoxLayout.Y_AXIS)); JLabel echoLabel = new JLabel("Mode:"); final JComboBox echoComboBox = new JComboBox(); echoComboBox.addItem("Normal"); echoComboBox.addItem("Password"); echoComboBox.addItem("No Echo");
echoLineEdit = new JTextField(); //////////////////
JPanel validatorGroup = new JPanel(new GridBagLayout()); validatorGroup.setBorder(BorderFactory.createTitledBorder("Validator")); validatorGroup.setLayout(new BoxLayout(validatorGroup, BoxLayout.Y_AXIS)); JLabel validatorLabel = new JLabel("Type:"); final JComboBox validatorComboBox = new JComboBox(); validatorComboBox.addItem("No validator"); validatorComboBox.addItem("Integer validator"); validatorComboBox.addItem("Double validator");
validatorLineEdit = new JTextField(); ////////////////////
JPanel alignmentGroup = new JPanel(new GridBagLayout()); alignmentGroup.setBorder(BorderFactory.createTitledBorder("Alignment")); JLabel alignmentLabel = new JLabel("Type:"); final JComboBox alignmentComboBox = new JComboBox(); alignmentComboBox.addItem("Left"); alignmentComboBox.addItem("Centered"); alignmentComboBox.addItem("Right");
alignmentLineEdit = new JTextField();
///////////////////////////////////////
JPanel inputMaskGroup = new JPanel(); inputMaskGroup.setBorder(BorderFactory.createTitledBorder("Input mask")); inputMaskGroup.setLayout(new BoxLayout(inputMaskGroup, BoxLayout.Y_AXIS)); JLabel inputMaskLabel = new JLabel("Type:"); final JComboBox inputMaskComboBox = new JComboBox(); inputMaskComboBox.addItem("No mask"); inputMaskComboBox.addItem("Phone number"); inputMaskComboBox.addItem("ISO date"); inputMaskComboBox.addItem("License key");
inputMaskLineEdit = new JTextField();
///////////////////////////////////////
JPanel accessGroup = new JPanel(new GridBagLayout()); accessGroup.setBorder(BorderFactory.createTitledBorder("Access")); JLabel accessLabel = new JLabel("Read-only:"); final JComboBox accessComboBox = new JComboBox(); accessComboBox.addItem("False"); accessComboBox.addItem("True");
accessLineEdit = new JTextField();
////////////////////////////////////////////
echoComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { echoChanged(echoComboBox.getSelectedIndex()); } });
validatorComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { validatorChanged(validatorComboBox.getSelectedIndex()); } });
alignmentComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { alignmentChanged(alignmentComboBox.getSelectedIndex()); } });
inputMaskComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { inputMaskChanged(inputMaskComboBox.getSelectedIndex()); } });
accessComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { accessChanged(accessComboBox.getSelectedIndex()); } });
///////////////////////////////////////////// GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; echoGroup.add(echoLabel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 1; echoGroup.add(echoComboBox, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.weightx = 1; echoGroup.add(echoLineEdit, gbc);
////////////////////////////////////////// gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; validatorGroup.add(validatorLabel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.weightx = 1; validatorGroup.add(validatorComboBox, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 2; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; validatorGroup.add(validatorLineEdit, gbc); ///////////////////////////////////////////////// gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; alignmentGroup.add(alignmentLabel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 1; alignmentGroup.add(alignmentComboBox, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; alignmentGroup.add(alignmentLineEdit, gbc);
gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; inputMaskGroup.add(inputMaskLabel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 1; inputMaskGroup.add(inputMaskComboBox, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; inputMaskGroup.add(inputMaskLineEdit, gbc);
gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; accessGroup.add(accessLabel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 1; accessGroup.add(accessComboBox, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.weighty = 1; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; accessGroup.add(accessLineEdit, gbc);
////////////////////////////////////////////// getContentPane().setLayout(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; gbc.fill = GridBagConstraints.BOTH; getContentPane().add(echoGroup, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.weightx = 1; gbc.fill = GridBagConstraints.BOTH; getContentPane().add(validatorGroup, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 2; gbc.weightx = 1; gbc.fill = GridBagConstraints.BOTH; getContentPane().add(alignmentGroup, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 1; gbc.fill = GridBagConstraints.BOTH; getContentPane().add(inputMaskGroup, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 1; gbc.fill = GridBagConstraints.BOTH; getContentPane().add(accessGroup, gbc); setTitle("Line Edits"); }
public void echoChanged(int index) { /*switch (index) { case 0: echoLineEdit.setEchoMode(QLineEdit.EchoMode.Normal); break; case 1: echoLineEdit.setEchoMode(QLineEdit.EchoMode.Password); break; case 2: echoLineEdit.setEchoMode(QLineEdit.EchoMode.NoEcho); }*/ }
public void validatorChanged(int index) { /*switch (index) { case 0: validatorLineEdit.setValidator(null); break; case 1: validatorLineEdit.setValidator(new QIntValidator(validatorLineEdit)); break; case 2: validatorLineEdit.setValidator(new QDoubleValidator(-999.0, 999.0, 2, validatorLineEdit)); }
validatorLineEdit.setText("");*/ }
public void alignmentChanged(int index) { /*switch (index) { case 0: alignmentLineEdit.setAlignment(new Qt.Alignment(Qt.AlignmentFlag.AlignLeft)); break; case 1: alignmentLineEdit.setAlignment(new Qt.Alignment(Qt.AlignmentFlag.AlignCenter)); break; case 2: alignmentLineEdit.setAlignment(new Qt.Alignment(Qt.AlignmentFlag.AlignRight)); }*/ }
public void inputMaskChanged(int index) { /*switch (index) { case 0: inputMaskLineEdit.setInputMask(""); break; case 1: inputMaskLineEdit.setInputMask("+99 99 99 99 99;_"); break; case 2: inputMaskLineEdit.setInputMask("0000-00-00"); inputMaskLineEdit.setText("00000000"); inputMaskLineEdit.setCursorPosition(0); break; case 3: inputMaskLineEdit.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#"); }*/ }
public void accessChanged(int index) { switch (index) { case 0: accessLineEdit.setEditable(true); break; case 1: accessLineEdit.setEditable(false); } }
public static void main(String args[]) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } JFrame.setDefaultLookAndFeelDecorated(true); JFrame f = new JavaLineEdits(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationByPlatform(true); f.setVisible(true); } }
|
Терпения не хватило работать с GridBagLayout, но смысл примерно тот же, может у Java задача проще, ибо приходится меньше изменять размеры. 1. В Qt все отступы уже продуманы, не надо работать с Insets 2. В Qt сложную GUI сетку можно сделать проще чем в SWING Теперь диагноз: в целом после растягиваний Qt программки остаются приятные ощущения, усталости не наблюдается. При работе с SWING программой весь экран содрагается в очень частой перерисовке (перерисовывается не только окно программы, но и весь рабочий стол и его приложения), остается ощущение неполноценности и ущербности, хочется быстрей бежать от такоо приложения. Это сообщение отредактировал(а) Platon - 11.3.2008, 09:02
|